我有一个可以用 SwiftyJSON 解析的 json:
if let title = json["items"][2]["title"].string {
println("title : \(title)")
}
完美运行。
但我无法遍历它。我尝试了两种方法,第一种是
// TUTO :
//If json is .Dictionary
for (key: String, subJson: JSON) in json {
...
}
// WHAT I DID :
for (key: "title", subJson: json["items"]) in json {
...
}
XCode 不接受 for 循环声明。
第二种方法:
// TUTO :
if let appArray = json["feed"]["entry"].arrayValue {
...
}
// WHAT I DID :
if let tab = json["items"].arrayValue {
...
}
XCode 不接受 if 语句。
我做错了什么?
最佳答案
如果你想循环遍历 json["items"]
数组,试试:
for (key, subJson) in json["items"] {
if let title = subJson["title"].string {
println(title)
}
}
至于第二种方法,.arrayValue
返回non 可选
数组,你应该使用.array
代替:
if let items = json["items"].array {
for item in items {
if let title = item["title"].string {
println(title)
}
}
}
https://stackoverflow.com/questions/28365939/