IOS Study Notes 38 -- use NSJSONSerialization

Source: Internet
Author: User

For details about NSJSONSerialization, the official documents include: You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON. an object that may be converted to JSON must have the following properties: The top level object is an NSArray or NSDictionary. all objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull. all dictionary keys are instances of NSStri Ng. numbers are not NaN or infinity. you can use NSJSONSerialization to convert JSON to a Foundation object or a Foundation object to JSON. the object to be converted to JSON must have the following attributes: the top-level object must be NSArray or NSDictionary. All objects must be NSString, NSNumber, NSArray, NSDictionary, and NSNull. The keys of all NSDictionary instances must be NSString numeric objects and cannot be non-numeric or infinite. Next let's look. how to use, first, how to generate data in JSON format: Here I will use code snippets in the project for a brief introduction, [cpp] NSDictionary * registerDic = [NSDictionary dictionaryWithObjectsAndKeys: uuid, @ "_ Id", userName, @ "login_name", password, @ "password", nil]; if ([NSJSONSerialization isValidJSONObject: registerDic]) {NSError * error; NSData * registerData = [NSJSONSerialization dataWithJSONObject: registerDic options: Unknown error: & error]; NSLog (@ "Register JSON: % @", [[NSString alloc] initWithData: registerData encoding: NSUTF8StringEncoding]);} the key in NSDictionary is the key in the json string, obje Ct is the value in the json string. isValidJSONObject: The method is to check whether the Foundation object can be legally converted to a JSON object, and the dataWithJSONObject: options: error method is to convert the Foundation object to a JSON object, the NSJSONWritingPrettyPrinted parameter is used to format the output of the generated json data, which is highly readable. If this parameter is not set, the output json string is a whole line. Parse json format data returned by the server: [cpp] NSDictionary * resultJSON = [NSJSONSerialization JSONObjectWithData: resultData options: kNilOptions error: & error]; obtain the value of the returned string whose key is status: [cpp] NSString * status = [resultJSON objectForKey: @ "status"]; The above briefly introduces the use of NSJSONSerilazation, which is not very comprehensive. I will explain it in detail later.

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.