我从web服务获取国家列表。接受它后,我用这个代码来处理它:Swift 2解析Json作为数组的可选
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
// triggering callback function that should be processed in the call
// doing logic
} else {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? AnyObject {
completion(json)
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON string: \(jsonStr)")
}
}
这表看起来是后(它在这部分NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? AnyObject
结束):
Optional((
{
"country_code" = AF;
"dial_code" = 93;
id = 1;
name = Afghanistan;
},
{
"country_code" = DZ;
"dial_code" = 213;
id = 3;
name = Algeria;
},
{
"country_code" = AD;
"dial_code" = 376;
id = 4;
name = Andorra;
}
))
我现在这个JSON对象转换为数组(或某种程度上NSDictionary)并循环它。有人可以建议如何?
我会测试它,并接受这个答案,如果它的工作!谢谢! – bla0009
它像一个魅力,感谢解释! – bla0009