Three JSON methods for iOS parsing APIs of the National Meteorological Administration

Source: Internet
Author: User

Weather forecast interface provided by the National Meteorological Administration

There are three interface addresses:

Http://www.weather.com.cn/data/sk/101010100.html

Http://www.weather.com.cn/data/cityinfo/101010100.html

Http://m.weather.com.cn/data/101010100.html

The third interface is more detailed. It provides 6-day weather. For details about the information returned by the API, see the open-source free weather forecast API and code for all regions in China !! (Provided by the National Meteorological Administration), each city in China corresponds to this ID. Based on the changed ID, we can parse the corresponding weather in each city;


JSON is simple and easy to use as a popular file format. It is faster to transmit than XML on mobile phones. Before ios5, Apple did not provide library file support for JSON parsing, however, there are some experts who only use the objective-C library to parse JSON files. iOS and Apple provide native support class nsjsonserialization for JSON. This article will introduce touchjson.
The native JSON methods supported by sbjson and ios5 parse the APIs of the National Meteorological Administration. touchjson and sbjson need to download their libraries.

Touchjson http://download.csdn.net/detail/duxinfeng2010/4484144.

Sbjson http://download.csdn.net/detail/duxinfeng2010/4484842

1. Create a new project called jsonthreedemo; file-> New-> Project
-> Single view application-> next. Do not check use automatic refrence counting if you do not use arc. Otherwise, an error is reported in the library file during running.



2. to use the touchjson library, add the header file # import "cjsondeserializer. H ", to use sbjson, add the header file # import" sbjson. H "then open XIB and add three buttons to add three methods


-(Ibaction) buttonpressedone :( ID) sender;

-(Ibaction) buttonpressedtwo :( ID) sender;

-(Ibaction) buttonpressedthree :( ID) sender;

3. The three parsing methods are similar.

Touchjson database parsing Beijing weather

-(Ibaction) buttonpressedone :( ID) sender {// get API interface nsurl * url = [nsurl urlwithstring: @ "http://m.weather.com.cn/data/101010100.html"]; // define an nserror object, used to capture the error message nserror * error; // nsstring * jsonstring = [nsstring stringwithcontentsofurl: URL encoding: nsutf8stringencoding error: & error]; // nslog (@ "jsonstring ---> % @", jsonstring); // store the parsed content in the dictionary. The encoding format is utf8, prevent garbled nsdictionary * rootdic = [[cjsondeserializer deserializer] deserialize: [jsonstring datausingencoding: nsutf8stringencoding] error: & error]; // because the returned JSON file contains two layers, go to the second-level category and put it in the dictionary. Go to 0 nsdictionary * weatherinfo = [rootdic objectforkey: @ "weatherinfo"]; // print the value of nslog (@ "the weather condition of today is % %%@: %", [weatherinfo objectforkey: @ "date_y"], [weatherinfo objectforkey: @ "Week"], [weatherinfo objectforkey: @ "city"], [weatherinfo objectforkey: @ "weather1"], [weatherinfo objectforkey: @ "temp1"]);}

Sbjson database, parse the weather in Nanyang, and change the ID number of the city.

-(Ibaction) buttonpressedtwo :( ID) sender {nsurl * url = [nsurl urlwithstring: @ "http://m.weather.com.cn/data/101180701.html"]; nserror * error = nil; nsstring * jsonstring = [nsstring stringwithcontentsofurl: URL encoding: nsutf8stringencoding error: & error]; sbjsonparser * parser = [[sbjsonparser alloc] init]; nsdictionary * rootdic = [parser objectwithstring: jsonstring error: & error]; nsdictionary * weatherinfo = [rootdic objectforkey: @ "weatherinfo"]; nslog (@ "today's weather conditions are: % @%@% @", [weatherinfo objectforkey: @ "date_y"], [weatherinfo objectforkey: @ "Week"], [weatherinfo objectforkey: @ "city"], [weatherinfo objectforkey: @ "weather1"], [weatherinfo objectforkey: @ "temp1"]);}

Native JSON parsing supported by ios5, weather in Xinyang City

-(Ibaction) buttonpressedthree :( ID) sender {nserror * error; // load an nsurl object nsurlrequest * request = [nsurlrequest requestwithurl: [nsurl urlwithstring: @ "http://m.weather.com.cn/data/101180601.html"]; // put the request URL data in the nsdata object nsdata * response = [nsurlconnection sendsynchronousrequest: Request returningresponse: Nil error: Nil]; // nsdictionary * weatherdic = [nsjsonserialization jsonobjectwithdata: response options: nsjsonreadingmutablaves error: & error]; // The data stored in the weatherdic dictionary is also of the dictionary type, from which the key value is nsdictionary * weatherinfo = [weatherdic objectforkey: @ "weatherinfo"]; nslog (@ "Today is %%%%%@ the weather condition is: %%%@", [weatherinfo objectforkey: @ "date_y"], [weatherinfo objectforkey: @ "Week"], [weatherinfo objectforkey: @ "city"], [weatherinfo objectforkey: @ "weather1"], [weatherinfo objectforkey: @ "temp1"]); // print the nslog (@ "the content in weatherinfo dictionary is --- >%@", [weatherinfo description]);}

If we want to obtain more information, take the value directly from the dictionary.

We used this class method.

+ (Nsdata
*) Sendsynchronousrequest :( nsurlrequest
*) Request returningresponse :( nsurlresponse
**) Response error :( nserror
**) Error

  • The URL request to be loaded. this request object, as part of the initialization process, is deeply copied (deep-copied ). after this method is returned, modifying the request will not affect the Request Used in the loading process.
  • The reponse output parameter, which is returned by the URL returned by the server.
  • Error output parameter. If an error occurs during request processing, it is null if there is no error.

It returns a download URL request. If the connection fails or the creation fails, Nil is returned.


4. Running result (if you want to know the value of each string and dictionary, you only need nslog to print the output ):



5. It takes some time to parse the value. An application crashes when the value is taken and the obtained value is incorrect.

Sometimes we get such data from the dictionary, which is depressing and does not display Chinese characters. In this case, we put the data in the dictionary and the encoding method is utf8, the value is printed in Chinese.

After the data is parsed, I want to take the value as follows,

Nsdictionary * weatherinfo = [rootdicobjectforkey: @ "weatherinfo"];

Nsarray * weatherarray = [rootdicobjectforkey: @ "weatherinfo"];

For (nsdictionary * dicin weatherarray ){

Nslog (@ "-----> % @", DIC );

}

The printed DIC data is like this.

This is the second layer of data in the JSON file and put it into an array. Then, a dictionary object is defined to traverse and retrieve the stored data in the array.

Nslog (@ "-----> % @", [dicobjectforkey: @ "city"]); to retrieve the value of city, but the application crashes.


This problem occurs because the stored values and values of the parsed data are incorrect;


Source code: http://download.csdn.net/detail/duxinfeng2010/4484818

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.