zoukankan      html  css  js  c++  java
  • SWIFT解析天气JSON格式

    访问以下链接可以得到京城当天的天气:http://www.weather.com.cn/adat/sk/101010100.html

    返回的JSON格式如下:

    {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"9","WD":"西南风","WS":"2级","SD":"22%","WSE":"2","time":"10:35","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1015"}}

    好吧,接下来我就建立一个playground并敲入以下代码,暂无把所有的错误设为nil:

    var url = NSURL(string: "http://www.weather.com.cn/adat/sk/101010100.html")

    var data = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingUncached, error: nil)

    var json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: nil)

    //解析JSON数据

    var weatherinfo:AnyObject? = json?.objectForKey("weatherinfo")

    var cityId:NSString = weatherinfo?.objectForKey("cityid") as NSString

    var city:NSString = weatherinfo?.objectForKey("city") as NSString

    var wind:NSString = weatherinfo?.objectForKey("WD") as NSString

    playground右边会打印出如下消息:

  • 相关阅读:
    [收集]Grid Animation-01
    陕西航天大学横幅动态测试
    IPC机制
    Android使用权限
    DDMS
    看懂UML类图和时序图
    Frameworks detected: Android framework is detected in the project
    CoordinatorLayout父布局的Behavior
    Android权限记录
    AndroidManifest.xml文件中属性记录
  • 原文地址:https://www.cnblogs.com/foxting/p/4474727.html
Copyright © 2011-2022 走看看