Swift parsing weather JSON format

Source: Internet
Author: User

Visit the following link to get the weather of the capital day: http://www.weather.com.cn/adat/sk/101010100.html

The JSON format returned is as follows:

{"Weatherinfo": {"City": "Beijing", "Cityid": "101010100", "temp": "9", "WD": "Southwest Wind", "WS": "Level 2", "SD": "22%", "WSE": "2", "Time": " 10:35 "," Isradar ":" 1 "," Radar ":" JC_RADAR_AZ9010_JB "," NJD ":" No Live "," qy ":" 1015 "}}

OK, so I'll build a playground and typing the following code, without setting all the errors to 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)

Parsing JSON data

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

The following message is printed on the right side of playground:

Swift parsing weather JSON format

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.