到目前为止,我已达到这个阶段
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary;
for item in json {
print (item);
}
输出:
(firstitem, 1)
(seconditem, 'something else')
我如何获得" seconditem
"?
答案 0 :(得分:2)
因为它是一本字典,你可以简单地称它为字典:
json["seconditem"]
如果你想保持你的循环,你可以遍历键值对:
for (key, value) in json {
print(value)
}
答案 1 :(得分:0)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary;
for item in json {
print (item);
}
print(json["secondItem"]) //prints value for "secondItem" key
} catch let error as NSError {
print("error: \(error)") // sth goes wrong, invalid json etc.
}
您将其转换为NSDictionary,因此您可以像使用普通字典一样访问其对象:json["key"]
或json.valueForKey("key")