Ios 使用SwiftyJSON解析嵌入的JSON
我已经做了一段时间了,但是不能正确地进行解析。我正在尝试解析此json文件:Ios 使用SwiftyJSON解析嵌入的JSON,ios,json,swift2,swifty-json,Ios,Json,Swift2,Swifty Json,我已经做了一段时间了,但是不能正确地进行解析。我正在尝试解析此json文件: { "order_history" : [ { "items" : [ { "id" : 284, "created" : [ 2016, 5, 26, 5, 27, 53 ], "updated" : [ 2016, 5, 27, 0, 31, 10 ], "sku" : "10-10-08-050", "name" : "Product one of set one"
{
"order_history" : [ {
"items" : [ {
"id" : 284,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 27, 0, 31, 10 ],
"sku" : "10-10-08-050",
"name" : "Product one of set one",
"description" : "",
"quantity" : 1.0,
"price" : 2000.0,
"total" : 2000.0,
"tax" : null,
"discount" : null
}, {
"id" : 285,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 27, 0, 31, 10 ],
"sku" : "10-22-12-247",
"name" : "Product 2 of set 1",
"description" : "",
"quantity" : 1.0,
"price" : 2300.0,
"total" : 2300.0,
"tax" : null,
"discount" : null
}, {
"id" : 286,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 27, 0, 31, 10 ],
"sku" : "10-22-12-249",
"name" : "Product 3 of set 1",
"description" : "",
"quantity" : 1.0,
"price" : 3700.0,
"total" : 3700.0,
"tax" : null,
"discount" : null
} ],
"items" : [ {
"id" : 288,
"created" : [ 2016, 5, 26, 5, 29, 51 ],
"updated" : [ 2016, 5, 27, 0, 31, 11 ],
"sku" : "JJ-02-00-042",
"name" : "Product 1 of set 2",
"description" : "",
"quantity" : 1.0,
"price" : 3000.0,
"total" : 3000.0,
"tax" : null,
"discount" : null
} ],
"items" : [ {
"id" : 310,
"created" : [ 2016, 5, 30, 7, 40, 41 ],
"updated" : [ 2016, 5, 30, 7, 40, 46 ],
"sku" : "J481",
"name" : "Product 1 set 3",
"description" : "",
"quantity" : 1.0,
"price" : 2200.0,
"total" : 2200.0,
"tax" : null,
"discount" : null
} ],
"items" : [ {
"id" : 311,
"created" : [ 2016, 5, 30, 7, 48, 39 ],
"updated" : [ 2016, 5, 30, 7, 48, 44 ],
"sku" : "JJ1",
"name" : "Product 2 set 3",
"description" : "",
"quantity" : 1.0,
"price" : 2200.0,
"total" : 2200.0,
"tax" : null,
"discount" : null
} ],
"items" : [ {
"id" : 312,
"created" : [ 2016, 5, 30, 9, 8, 31 ],
"updated" : [ 2016, 5, 30, 9, 8, 32 ],
"sku" : "J77",
"name" : "Product 3 in set 3",
"description" : "",
"quantity" : 1.0,
"price" : 2200.0,
"total" : 2200.0,
"tax" : null,
"discount" : null
} ]
}
]
}
因为现在我能从中得到的只是第一套产品。我想做的是得到所有三个集合以及“created”字段。我根本没有运气得到创建的字段。它只是空的
这就是我从json文件中获取数据的方式
for (_, myosin) in newJson["order_history"][0]["items"] {
if let set = myosin["name"].string {
//products is a string crated somewhere up there ^
products.appendContentsOf("\n" + name)
}
}
谢谢你在这方面的帮助 您正在将“name”字段解码为set
变量,但随后尝试使用一个不存在的name
变量。您还使用了appendContentsOf
,但它应该是append
var products = [String]()
for (_, myosin) in newJson["order_history"][0]["items"] {
if let set = myosin["name"].string {
products.append("\n" + set)
}
}
print(products)
要获取每个字典的“已创建”数组,请执行以下操作:
var createdArrays = [[Int]]()
for (_, myosin) in newJson["order_history"][0]["items"] {
if let created = myosin["created"].arrayObject as? [Int] {
createdArrays.append(created)
}
}
print(createdArrays)
老兄,前两个项目的字典里没有名字字段,我知道这就是为什么我不认为这是可能的,但我想我会问。这是我被告知要分析的。谢谢@Eric D我很感激。我仍然习惯于用swift编写代码