As a lightweight data interchange format, JSON is gradually replacing XML as a common format for network data.
Some JSON code format is confusing, you can use this "http://www.bejson.com/" Web site for JSON format checksum (click to open the link). This site not only detects errors in the JSON code, but it is convenient to display the data content in JSON in a view.
Starting with IOS5, Apple provides native support for JSON (Nsjsonserialization), but in order to be compatible with previous versions of iOS, you can use a Third-party library to parse JSON.
This article will introduce native JSON methods supported by Touchjson, Sbjson, Jsonkit, and iOS5 to parse the National Weather Service Api,touchjson and Sbjson need to download their libraries
Touchjson Package Download: Http://xiazai.jb51.net/201612/yuanma/TouchJson (jb51.net)
Sbjson Package Download: Http://xiazai.jb51.net/201612/yuanma/SBJson (jb51.net)
Jsonkit Package Download: Http://xiazai.jb51.net/201612/yuanma/JSONKit (jb51.net)
The following full program source pack download: HTTP://XIAZAI.JB51.NET/201612/YUANMA/SBJSONWZ (jb51.net)
Ps:
Weather forecast interface provided by National Weather Service
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 information is more detailed, provided is 6 days of weather, information about the return of the API see open source free weather Interface API and all regions of the country code!! (National Weather Service), the country's cities corresponding to this ID number, according to change the ID good we can resolve the corresponding weather in each city;
Here are four methods of parsing JSON:
First create a new project (be careful not to select the ARC mechanism) to add the following controls:
As shown in the figure above. The following exhibits the program code:
In file ViewController.h:
#import <UIKit/UIKit.h>
@interface viewcontroller:uiviewcontroller
@property (retain, nonatomic) Iboutlet Uitextview *txtview;
-(Ibaction) Btnpresstouchjson: (ID) sender;
-(Ibaction) Btnpresssbjson: (ID) sender;
-(Ibaction) Btnpressios5json: (ID) sender;
-(Ibaction) Btnpressjsonkit: (ID) sender;
Main code in File VIEWCONTROLLER.M:
(1) Using Touchjson Parsing method: (Need to import Package: #import "Touchjson/json/cjsondeserializer.h")
(2) Using Sbjson Parsing method: (Need to import Package: #import "Sbjson/sbjson.h")
Use Sbjson to parse Nanyang Weather
-(ibaction) Btnpresssbjson: (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"];
Txtview.text = [NSString stringwithformat:@ "Today is%@%@%@ weather conditions are:%@%@", [Weatherinfo objectforkey:@ "Date_y"],[ Weatherinfo objectforkey:@ "Week"],[weatherinfo objectforkey:@ "City", [Weatherinfo objectforkey:@ "Weather1"], [ Weatherinfo objectforkey:@ "Temp1"];
(3) Using IOS5 Nsjsonserialization method parsing: (No import package, IOS5 support, low version iOS not supported)
(ibaction) Btnpressios5json: (ID) sender {nserror *error; Loads a Nsurl object nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@] http://m.weather.com.cn/
Data/101180601.html "]]; Place the requested URL data in the NSData object NSData *response = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error
: nil]; IOS5 parsing class nsjsonserialization from response to the dictionary nsdictionary *weatherdic = [nsjsonserialization
Jsonobjectwithdata:response options:nsjsonreadingmutableleaves error:&error];
Nsdictionary *weatherinfo = [weatherdic objectforkey:@ "Weatherinfo"]; Txtview.text = [NSString stringwithformat:@ "Today is%@%@%@ weather conditions are:%@%@", [Weatherinfo objectforkey:@ "Date_y"],[ Weatherinfo objectforkey:@ "Week"],[weatherinfo objectforkey:@ "City", [Weatherinfo objectforkey:@ "Weather1"], [
Weatherinfo objectforkey:@ "Temp1"];
NSLog (@ "Weatherinfo the contents of the dictionary is--"%@ ", weatherdic); }
(4) Using Jsonkit parsing method: (Import Package: #import "jsonkit/jsonkit.h")
-(ibaction) Btnpressjsonkit: (ID) Sender {//If JSON is "single layer", that is, value is a string, a number,
You can use the objectfromjsonstring nsstring *json1 = @ "{\ A\": 123, \ "b\": \ "Abc\"} ";
NSLog (@ "json1:%@", Json1);
Nsdictionary *data1 = [Json1 objectfromjsonstring];
NSLog (@ "json1.a:%@", [Data1 objectforkey:@ "a"]);
NSLog (@ "json1.b:%@", [data1 objectforkey:@ "B"]);
[Json1 release]; If JSON is nested, meaning that there is an array, object in value, and if you use Objectfromjsonstring again, the program may have an error (the test results show that the error occurs when using JSON generated by the network or the resulting php/json_encode). However, when using nsstring-defined JSON strings, parsing succeeds), it is best to use objectfromjsonstringwithparseoptions:nsstring *json2 = @ "{\ A\": 123, \ "b\": \ "abc\
", \" c\ ": [456, \" hello\ "], \" d\ ": {\" name\ ": \" john \ ", \" age\ ": \" 32\ "}";
NSLog (@ "json2:%@", Json2);
Nsdictionary *data2 = [Json2 Objectfromjsonstringwithparseoptions:jkparseoptionlooseunicode];
NSLog (@ "json2.c:%@", [data2 objectforkey:@ "C"]);
NSLog (@ "json2.d:%@", [data2 objectforkey:@ "D"]);
[Json2 release]; }
In addition, because IOS5 added the JSON parsing API, we tested it with the other five open source JSON parsing libraries, and the following are the results of the tests.
The test objects we chose include the following frameworks, where Nsjsonserialization is the new JSON-parsed API for the IOS5 system and requires a iOS5 environment, which should be masked if you are testing in a lower version.
-[Sbjson (Json-framework)] (http://code.google.com/p/json-framework/)
-[Touchjson (from Touchcode)] (http:// code.google.com/p/touchcode/)
-[Yajl (Objective-c Bindings)] (HTTP://GITHUB.COM/GABRIEL/YAJL-OBJC)
-[ Jsonkit] (https://github.com/johnezang/JSONKit)
-[Nextivejson] (Https://github.com/nextive/NextiveJson)
-[nsjsonserialization] (http://developer.apple.com/library/ios/#documentation/foundation/reference/ nsjsonserialization_class/reference/reference.html#//apple_ref/doc/uid/tp40010946)
We have selected four files that contain JSON-formatted data for testing. Each file carries out a 100 parsing action that compares the parsing time.
.....
The results of the test show that the system's API parsing speed is the fastest, we choose to use in the project, but also the application of a more extensive Sbjson resolution speed is the penultimate difference, I am surprised.
Closer to the system API should be jsonkit.
There is no comparison between the Open interface and the usage of the API, if simply based on the above analytic Speed test:
1:IOS5 should select the system's API for
2: Should not use the system API should choose Jsonkit
The above is a small set of iOS to introduce the JSON parsing example method (four methods) of all the narrative, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!