Four ways to parse JSON in "go" iOS

Source: Internet
Author: User
Tags uikit
<span id="Label3"></p><p><p>Original Url: http://blog.csdn.net/enuola/article/details/7903632</p></p><p><p>As a lightweight data interchange format, JSON is gradually replacing XML as a common format for network Data.</p></p><p><p>Some JSON code format is confusing, you can use this "http://www.bejson.com/" Web site for JSON format check (click the open link). This web site not only detects errors in JSON code, but also displays the contents of the data in JSON in the form of a VIEW.</p></p><p><p>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 the Json.</p></p><p><p>This article describes the <strong>native JSON methods supported by touchjson, sbjson, jsonkit, and IOS5</strong> , and parses the National Weather bureau Api,touchjson and Sbjson to download their libraries</p></p><p class="p2"><p class="p2">Touchjson Package Download: http://download.csdn.net/detail/enuola/4523169</p></p><p class="p2"><p class="p2">Sbjson Package Download: http://download.csdn.net/detail/enuola/4523177</p></p><p class="p2"><p class="p2">Jsonkit Package Download: http://download.csdn.net/detail/enuola/4523160</p></p><p class="p2"><p class="p2">The following full program source code package download: http://download.csdn.net/detail/enuola/4523223<br></p></p><p><p>Ps:</p></p><p class="p1"><p class="p1">Weather interface provided by the National Weather</p></p><p class="p1"><p class="p1">There are three interface addresses:</p></p><p class="p2"><p class="p2"><span class="s1">Http://www.weather.com.cn/data/sk/101010100.html</span></p></p><p class="p2"><p class="p2"><span class="s1">Http://www.weather.com.cn/data/cityinfo/101010100.html</span></p></p><p class="p2"><p class="p2"><span class="s1">Http://m.weather.com.cn/data/101010100.html</span></p></p><p class="p2"><p class="p2">The third interface information is more detailed, provide 6 days of weather, about the information returned by the api, please see the Open source Free weather API and all the country code!! (national Weather Bureau provides), the country each city corresponds to this one ID number, according to change the ID good we can parse out each city corresponds to the weather;</p></p><p class="p2"><p class="p2">Here are four ways to parse json:</p></p><p><p>First create a new project, (be careful not to select the Arc Mechanism) to add the following controls:</p></p><p><p></p></p><p><p>as shown in. The program code is shown below:</p></p><p><p>In File ViewController.h:</p></p><strong><strong>[cpp]</strong></strong>View Plain<span data-mod="popu_168"><span data-mod="popu_168"><span data-mod="popu_168">Copy</span></span></span> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><span class="preprocessor">#import <UIKit/UIKit.h></span></li> <li></li> <li class="alt">@interface Viewcontroller:uiviewcontroller</li> <li></li> <li class="alt">@property (retain, nonatomic) iboutlet uitextview *txtview;</li> <li></li> <li class="alt">-(ibaction) btnpresstouchjson: (id) sender;</li> <li>-(ibaction) btnpresssbjson: (id) sender;</li> <li class="alt">-(ibaction) btnpressios5json: (id) sender;</li> <li>-(ibaction) btnpressjsonkit: (id) sender;</li> <li class="alt"></li> <li>@end</li> </ol> </ol><p><p>Main code in File viewcontroller.m:</p></p><p><p>(1) using <strong>touchjson</strong> parsing method: (need to import package: #import "touchjson/json/cjsondeserializer.h")</p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plain<span data-mod="popu_168"><span data-mod="popu_168"><span data-mod="popu_168">Copy</span></span></span> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><span class="comment">Use Touchjson to analyze the weather in Beijing</span></li></li> <li><li>-(ibaction) btnpresstouchjson: (id) Sender {</li></li> <li class="alt"><li class="alt"><span class="comment">//get API Interface</span></li></li> <li><li>Nsurl *url = [nsurl urlwithstring:@<span class="string">"http://m.weather.com.cn/data/101010100.html"]; </span></li></li> <li class="alt"><li class="alt"><span class="comment">//define a Nserror object for capturing error messages</span></li></li> <li><li>Nserror *error;</li></li> <li class="alt"><li class="alt">NSString *jsonstring = [nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];</li></li> <li><li>NSLog (@<span class="string">"jsonstring--->%@", jsonstring); </span></li></li> <li class="alt"><li class="alt"><span class="comment">//the parsed content is stored in the dictionary, the encoding format is UTF8, and prevents garbled characters when the value is not taken</span> .</li></li> <li><li>Nsdictionary *rootdic = [[cjsondeserializer deserializer] deserialize:[jsonstring datausingencoding: nsutf8stringencoding] error:&error];</li></li> <li class="alt"><li class="alt"><span class="comment">//because The JSON file returned has two layers, go to the second layer of content into the dictionary</span></li></li> <li><li>Nsdictionary *weatherinfo = [rootdic objectforkey:@<span class="string">"weatherinfo"]; </span></li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"weatherinfo--->%@", weatherinfo); </span></li></li> <li><li><span class="comment">//value Printing</span></li></li> <li class="alt"><li class="alt">Txtview.text = [nsstring stringwithformat:@<span class="string">"today is%@%@%@ weather conditions are:%@%@", [weatherinfo objectforkey:@<span class="string">"date_y"],[ Weatherinfo objectforkey:@<span class="string">"week"],[weatherinfo objectforkey:@<span class="string">"city"], [weatherinfo objectforkey:@]<span class="string">  Weather1 "], [weatherinfo objectforkey:@<span class="string">" Temp1 "]; </span></span></span></span></span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>(2) using <strong>sbjson</strong> parsing method: (need to import package: #import "sbjson/sbjson.h")</p></p><p><p></p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plain<span data-mod="popu_168"><span data-mod="popu_168"><span data-mod="popu_168">Copy</span></span></span> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt"><span class="comment">Using Sbjson to analyze the weather in Nanyang</span></li></li> <li><li>-(ibaction) btnpresssbjson: (id) Sender {</li></li> <li class="alt"><li class="alt">Nsurl *url = [nsurl urlwithstring:@<span class="string">"http://m.weather.com.cn/data/101180701.html"]; </span></li></li> <li><li>Nserror *error = nil;</li></li> <li class="alt"><li class="alt">NSString *jsonstring = [nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];</li></li> <li><li>Sbjsonparser *parser = [[sbjsonparser alloc] init];</li></li> <li class="alt"><li class="alt"></li></li> <li><li>Nsdictionary *rootdic = [parser objectwithstring:jsonstring error:&error];</li></li> <li class="alt"><li class="alt">Nsdictionary *weatherinfo = [rootdic objectforkey:@<span class="string">"weatherinfo"]; </span></li></li> <li><li>Txtview.text = [nsstring stringwithformat:@<span class="string">"today is%@%@%@ weather conditions are:%@%@", [weatherinfo objectforkey:@<span class="string">"date_y"],[ Weatherinfo objectforkey:@<span class="string">"week"],[weatherinfo objectforkey:@<span class="string">"city"], [weatherinfo objectforkey:@]<span class="string">  Weather1 "], [weatherinfo objectforkey:@<span class="string">" Temp1 "]; </span></span></span></span></span></span></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>(3) parsing with <strong>IOS5 self-parsing class nsjsonserialization</strong> method: (no import package, IOS5 support, low version iOS not supported)</p></p><p><p></p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plain<span data-mod="popu_168"><span data-mod="popu_168"><span data-mod="popu_168">Copy</span></span></span> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) btnpressios5json: (id) Sender {</li></li> <li><li></li></li> <li class="alt"><li class="alt">Nserror *error;</li></li> <li><li><span class="comment">//load a Nsurl object</span></li></li> <li class="alt"><li class="alt">Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@<span class="string">"http://m.weather.com.cn/data/  101180601.html "]; </span></li></li> <li><li><span class="comment">//put The requested URL data into the NSData object</span></li></li> <li class="alt"><li class="alt">NSData *response = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];</li></li> <li><li><span class="comment">//ios5 self-contained parsing class Nsjsonserialization parse data from response into a dictionary</span></li></li> <li class="alt"><li class="alt">Nsdictionary *weatherdic = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves error :&error];</li></li> <li><li>Nsdictionary *weatherinfo = [weatherdic objectforkey:@<span class="string">"weatherinfo"]; </span></li></li> <li class="alt"><li class="alt">Txtview.text = [nsstring stringwithformat:@<span class="string">"today is%@%@%@ weather conditions are:%@%@", [weatherinfo objectforkey:@<span class="string">"date_y"],[ Weatherinfo objectforkey:@<span class="string">"week"],[weatherinfo objectforkey:@<span class="string">"city"], [weatherinfo objectforkey:@]<span class="string">  Weather1 "], [weatherinfo objectforkey:@<span class="string">" Temp1 "]; </span></span></span></span></span></span></li></li> <li><li>NSLog (@<span class="string">"weatherinfo dictionary content is--"%@ ", weatherdic); </span></li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p>(4) parsing method using <strong>Jsonkit</strong> : (need to import package: #import "jsonkit/jsonkit.h")</p></p><p><p></p></p><p><p></p></p><strong><strong>[cpp]</strong></strong>View Plain<span data-mod="popu_168"><span data-mod="popu_168"><span data-mod="popu_168">Copy</span></span></span> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) btnpressjsonkit: (id) Sender {</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">//if JSON is "single-layer", that is, value is a string, a number, you can use objectfromjsonstring</span></li></li> <li><li>NSString *json1 = @<span class="string">"{\" a\ ": 123, \" b\ ": \" abc\ "}"; </span></li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"json1:%@", json1); </span></li></li> <li><li>Nsdictionary *data1 = [json1 objectfromjsonstring];</li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"json1.a:%@", [data1 objectforkey:@<span class="string">"a"]); </span></span></li></li> <li><li>NSLog (@<span class="string">"json1.b:%@", [data1 objectforkey:@<span class="string">"b"]); </span></span></li></li> <li class="alt"><li class="alt">[json1 release];</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">//if JSON is nested, that is, there is an array, object in value, and if you use Objectfromjsonstring again, the program may error (test results indicate: use of the network or the resulting php/json_ Encode generates an error when the JSON is generated, but when using a JSON string defined by nsstring, parsing succeeds, It is best to use objectfromjsonstringwithparseoptions:</span></li></li> <li><li>NSString *json2 = @<span class="string">"{\" a\ ": 123, \" b\ ": \" abc\ ", \" c\ ": [456, \" hello\ "], \" d\ ": {\" name\ ": \" Zhang San \ ", \" age\ ": \" 32\ "}}"; </span></li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"json2:%@", json2); </span></li></li> <li><li>Nsdictionary *data2 = [json2 objectfromjsonstringwithparseoptions:jkparseoptionlooseunicode];</li></li> <li class="alt"><li class="alt">NSLog (@<span class="string">"json2.c:%@", [data2 objectforkey:@<span class="string">"c"]); </span></span></li></li> <li><li>NSLog (@<span class="string">"json2.d:%@", [data2 objectforkey:@<span class="string">"d"]); </span></span></li></li> <li class="alt"><li class="alt">[json2 release];</li></li> <li><li>}</li></li> </ol><p><p><br>In addition, since IOS5 has added the JSON parsing api, we have tested it with five other open source JSON parsing libraries, and the test results are as Follows.</p></p><p><p></p></p><p><p>The test object We selected contains the following frameworks, where Nsjsonserialization is the new JSON parsing API for the IOS5 system and requires a iOS5 environment, and if you test in a lower version, you should block the corresponding code Call.</p></p><p><p>-[sbjson (json-framework)] (http://code.google.com/p/json-framework/)</p></p><p><p>-[touchjson (from touchcode)] (http://code.google.com/p/touchcode/)</p></p><p><p>-[YAJL (objective-c bindings)] (http://github.com/gabriel/yajl-objc)</p></p><p><p>-[jsonkit] (https://github.com/johnezang/JSONKit)</p></p><p><p>-[nextivejson] (https://github.com/nextive/NextiveJson)</p></p><p><p>-[nsjsonserialization] (http://developer.apple.com/library/ios/#documentation/foundation/reference/ Nsjsonserialization_class/reference/reference.html#//apple_ref/doc/uid/tp40010946)</p></p><p><p>We have selected four files containing json-formatted data for Testing. Each file carries out 100 parsing actions, comparing the parsing Time.</p></p><p><p>.....</p></p><p><p>The test results show that the System's API parsing speed is the fastest, we choose to use in the project, but also the application of a wide range of Sbjson resolution of the Second-to-last difference, I am surprised.</p></p><p><p>Closer to the system API should be jsonkit.</p></p><p><p></p></p><p><p>There is no comparison of open interfaces and usage methods for apis, if only for testing based on the above parsing speed:</p></p><p><p>1:IOS5 should choose the System's API for</p></p><p><p>2: the system API should not be used should be selected Jsonkit</p></p><p><p>Four ways to parse JSON in "go" iOS</p></p></span>
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.