加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用swift解析json

发布时间:2020-12-14 07:02:16 所属栏目:百科 来源:网络整理
导读:let jsonObject = try NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers) print (jsonObject)let array = jsonObject as ! NSArray //读取数组中某个key所对应的所有值 print ( array .valueForKey( "text" ))
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers)
print(jsonObject)

let array = jsonObject as! NSArray

//读取数组中某个key所对应的所有值
print(array.valueForKey("text"))

//读取第一个元素
print(array[0])

//读取第一个元素的key对应的值
let text = array[0].valueForKey("text")
print(text)

//在使用if let语句的时候,swift会自动进行拆包
if let state = array[0].objectForKey("state") {
    print(state)
}
(
        {
        id = 1;
        state = closed;
        text = "Node 1";
    },{
        id = 2;
        state = open;
        text = "Node 2";
    },{
        id = 3;
        state = open;
        text = "Node 3";
    },{
        id = 4;
        state = open;
        text = "Node 4";
    }
)
(
    "Node 1","Node 2","Node 3","Node 4"
)
{
    id = 1;
    state = closed;
    text = "Node 1";
}
Optional(Node 1)
closed

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读