Ios 正确循环并检索SwiftyJSON中的值
我试图找到一种正确的方法来循环和检索SwiftyJSON中的值。 请参阅下面代码中的注释Ios 正确循环并检索SwiftyJSON中的值,ios,json,swift,swifty-json,Ios,Json,Swift,Swifty Json,我试图找到一种正确的方法来循环和检索SwiftyJSON中的值。 请参阅下面代码中的注释 audioReq.executeWithResultBlock({ response in let json = JSON(response.json) for (key, subJson) in json { if let title = subJson[key].string { //<--
audioReq.executeWithResultBlock({
response in
let json = JSON(response.json)
for (key, subJson) in json {
if let title = subJson[key].string { //<-- Loop does not work!
println(title) // It prints nothing!
}
}
if let title = json[0]["first_name"].string {
println(title) //<-- Works
}
if let title = json[0]["last_name"].string {
println(title) //<-- Works
}
if let title = json[0][1].string {
println(title) //<-- Does not work!
} // Prints nothing!
if let title = json[0]["id"].string {
println(title) //<-- Does not work!
} // Prints nothing!
println(response.json)
},
errorBlock: {(error:NSError!) -> Void in
println(error.localizedDescription)
})
您正在使用数组而不是字典。应该起作用的是
for (key, subJson) in json[0] {
if let title = subJson[key].string { //<-- Loop does not work!
println(title) // It prints nothing!
}
}
JSON响应的格式似乎不正确……您能解决这个问题吗?我已经联系了负责我正在使用的SDK的开发人员,他说服务器响应的样子可以:(由于格式不正确,它不能用作JSON,但它仍然可以在NSDictionary的帮助下使用。
for dict in json {
// go through dictionary elements here: first_name, last_name, id
// you can use same for loop as above
}