Traditional Culture Encyclopedia - Weather inquiry - How swift language nsurlsession asynchronously requests parsing json data

How swift language nsurlsession asynchronously requests parsing json data

//Method of parsing JSON

func loadJsonData(){

//Define the interface address for obtaining json data. The API interface for obtaining weather is defined here. Another advantage is that swift statements do not need to be forced to use ";" At the end of each statement.

var URL = NSURL(string:"/data/sk/ 10 1 1 10 10 10 1 . html ")

//get JSON data

var data = ns data . datawithcontentsofurl(URL,options:NSDataReadingOptions。 DataReadingUncached, error: zero)

var JSON:any object = NSJSONSerialization。 JSONObjectWithData (data, options: NSJSONReadingOptions. AllowFragments, error: zero)

//Parse to get the JSON field value

Varweatherinfo: anyobject = json.objectforkey ("weatherinfo")//JSON structure field name.

var city:any object = weather info . objectforkey(" city ")

//etc. to get the information of other fields, so I won't go into details here.

//Then assign the obtained json value to the corresponding control.

Tv.text = "City: \(city)" // Use \ () to embed the variable into the corresponding string.

}