前端之家收集整理的这篇文章主要介绍了
使用swift解析json,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers)
print(jsonObject)
let array = jsonObject as! NSArray
print(array.valueForKey("text"))
print(array[0])
let text = array[0].valueForKey("text")
print(text)
if let state = array[0].objectForKey("state") {
print(state)
}
(
{
id = 1;
state = closed;
@H_404_54@text = "Node 1";
},{
id = 2;
state = open;
@H_404_54@text = "Node 2";
},{
id = 3;
state = open;
@H_404_54@text = "Node 3";
},{
id = 4;
state = open;
@H_404_54@text = "Node 4";
}
)
(
"Node 1","Node 2","Node 3","Node 4"
)
{
id = 1;
state = closed;
@H_404_54@text = "Node 1";
}
Optional(Node 1)
closed
原文链接:https://www.f2er.com/swift/323924.html