![](/img/trans.png)
[英]How to Convert an Array of SwiftyJSON objects to a SwiftyJSON object?
[英]How to loop array of JSON object using SwiftyJSON?
我想循環一個JSON對象數組,但它一直給我錯誤
[
{
"key" : "501",
"latitude" : 3.0691697,
"longitude" : 333.64444639369011,
"distance" : 5,
},
{
"key" : "502",
"latitude" : 3.073096722364426,
"longitude" :12.4444000,
"distance" : 487,
}
]
代碼示例
if let value = response.result.value {
let json = JSON(value)
// This is the part Where I got stuck
for item in json {
print (item["latitude"]) // Keep giving me "Latitude is not a subscript of JSON"
}
}
如果我print item
以查看值,我將得到
("0", {
"latitude" : 3.0691697,
"longitude" : 333.64444639369011,
"key" : "501",
"distance" : 5
})
("1", {
"latitude" : 3.073096722364426,
"longitude" : 12.4444000,
"key" : "502",
"distance" : 487
})
我該怎么做才能解決這個問題?
您的keys
類型為String
。 這里的鍵是"0"
, "1"
等。
嘗試這個:
for (index,subJson):(String, JSON) in json {
//Do something you want
print (subJson["latitude"])
}
編輯:如果您不想使用index
,則可以使用_
替換index
例如:
for(_, subJson): (String, JSON) in json {
print(subJson["latitude"])
}
試試這段代碼
for (key0,json) in response.result.value as! Dictionary<String, AnyObject>
{
print("key0 \(key0), json \(json)")
for (key,val) in json as! Dictionary<String, AnyObject>
{
print("key \(key), val \(val)")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.