qq_31675471 2016-08-15 09:10 采纳率: 0%
浏览 971

swift 网络数据 json 解析

{"results":[{"location":{"id":"WS0E9D8WN298","name":"广州","country":"CN","path":"广州,广州,广东,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"大雨","code":"15","temperature":"31"},"last_update":"2016-08-15T15:05:00+08:00"}]}

如何进行json解析

  • 写回答

1条回答 默认 最新

  • MangoStyle 2016-08-15 10:26
    关注

    data =
    {
    "results": [
    {
    "location": {
    "id": "WS0E9D8WN298",
    "name": "广州",
    "country": "CN",
    "path": "广州,广州,广东,中国",
    "timezone": "Asia/Shanghai",
    "timezone_offset": "+08:00"
    },
    "now": {
    "text": "大雨",
    "code": "15",
    "temperature": "31"
    },
    "last_update": "2016-08-15T15:05:00+08:00"
    }
    ]
    }

        guard let jsonObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) else {
            return
        }
        let results = jsonObject["results"]
    
                        let info = NSDictionary()
        for result in results {
            let location = result["location"] as! NSDictionary
            info.setValue(location["id"], forKey: "id")
            info.setValue(location["name"], forKey: "name")
            info.setValue(location["country"], forKey: "country")
            info.setValue(location["path"], forKey: "path")
            info.setValue(location["timezone"], forKey: "timezone")
            info.setValue(location["timezone_offset"], forKey: "timezone_offset")
    
            let now = result["now"] as! NSDictionary
            info.setValue(now["text"], forKey: "text")
            info.setValue(now["code"], forKey: "code")
            info.setValue(now["temperature"], forKey: "temperature")
    
            info.setValue(result["last_update"], forKey: "last_update")
        }
    
        print(info)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办