IOS Study Notes (11)-JSON data parsing

Source: Internet
Author: User

In the previous iOS study-xml data parsing (9), we introduced xml data parsing. This article briefly introduces Json data parsing. JSON is JavaScript Object Natation. It is a lightweight data exchange format and is suitable for server-to-client interaction. Json syntax reference. Many third-party open-source projects, such as TouchJson, JSONKit, and SBJon, have been involved in JSON parsing on the iOS platform. Since iOS5.0, Apple SDK has released its own JSON solution NSJSONSerialization, this is a very useful JSON generation and resolution tool, which is much more efficient than other third-party open-source projects. For details, see.

You can view the image details.

NSJSONSerialization provides Json Data encapsulation and Json data parsing. NSJSONSerialization converts JSON data to NSDictionary or NSArray to unpack data, converts NSDictionary and NSArray objects to JSON data (you can call isValidJSONObject to determine whether NSDictionary and NSArray objects can be converted to JSON data) packets. This article will give a brief introduction.


Json data packets
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1", @ "key1", @ "value2", @ "key2", @ "value3", @ "key3 ", nil]; // isValidJSONObject determines whether the object can be constructed into a json object if ([NSJSONSerialization isValidJSONObject: dic]) {NSError * error; // create a json file from Data, NSJSONWritingPrettyPrinted: the white space of the specified JSON data to make the output more readable. NSData * jsonData = [NSJSONSerialization dataWithJSONObject: dic options: Internal error: & error]; NSString * json = [[NSString alloc] initWithData: jsonData encoding: NSUTF8StringEncoding]; NSLog (@ "json data: % @", json );}
Json data parsing

NSError * error; // load an NSURL object NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ "http://m.weather.com.cn/data/101120101.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]; NSDictionary * weatherInfo = [weatherDic objectForKey: @ "weatherinfo"]; NSString * text = [NSString stringWithFormat: @ "today's weather conditions are: % @ ", [weatherInfo objectForKey: @" date_y "], [weatherInfo objectForKey: @" week "], [weatherInfo objectForKey: @" city "], [weatherInfo objectForKey: @ "weather1"], [weatherInfo objectForKey: @ "temp1"]; NSLog (@ "weatherInfo: % @", text );
/**

* @ Author Zhang xingye * http://blog.csdn.net/xyz_lmn* iOS entry group: 83702688

* Android Development Group: 241395671

* My Sina Weibo:@ Zhang xingye tbow*/

Refer:

Http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946-CH1-DontLinkElementID_1

Http://www.w3school.com.cn/json/

Http://www.ibm.com/developerworks/cn/web/wa-lo-json/

Http://blog.sina.com.cn/s/blog_7018d3820101bdqz.html

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.