IOS Study Notes 38 -- use nsjsonserialization

Source: Internet
Author: User

In the previous study note "iOS Study Notes 33-xml parsing-kissxml usage", I introduced how to parse data in XML format. Today I will briefly introduce how to parse data in JSON format, JSON data structures are increasingly widely used with lightweight structures and good readability. Especially in mobile development, mobile phone traffic is a valuable resource, it is also required to use a lightweight data format for data transmission. Many third-party open-source projects, such as sbjson and jsonframwork, have been used for JSON parsing on the iOS platform. Since ios5.0, apple released the SDK's built-in JSON solution nsjsonserialization, which is a very useful JSON generation and resolution tool and is much more efficient than other third-party open-source projects. For details, see developer.
Guider.


About nsjsonserialization:

You useNSJSONSerializationClass 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 isNSArrayOrNSDictionary.

  • All objects are instancesNSString,NSNumber,NSArray,NSDictionary,
    OrNSNull.

  • All dictionary keys are instancesNSString.

  • 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 instances.
  • The key of all nsdictionary must be of the nsstring type.
  • Numeric objects cannot be non-numeric or infinite
Next, let's take a look at how to use it. First, we will give a brief introduction to how to generate data in JSON format: I will use code snippets in the project here. The following shows how to generate data in JSON format for login requests.
NSDictionary *registerDic = [NSDictionary dictionaryWithObjectsAndKeys:uuid,@"_id",userName,@"login_name",password,@"password", nil];    if ([NSJSONSerialization isValidJSONObject:registerDic]) {        NSError *error;        NSData *registerData = [NSJSONSerialization dataWithJSONObject:registerDic options:NSJSONWritingPrettyPrinted error:&error];        NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:registerData encoding:NSUTF8StringEncoding]);    }

The key in nsdictionary is the key in the JSON string, and the object 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, datawithjsonobject: Options: the error method is to convert the foundation object to a JSON object. The nsjsonwritingprettyprinted parameter is used to format 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:

NSDictionary *resultJSON = [NSJSONSerialization JSONObjectWithData:resultData options:kNilOptions error:&error];

Obtain the value of the returned string whose key is status:

NSString *status = [resultJSON objectForKey:@"status"];

The above section briefly introduces the use of nsjsonserilazation, which is not comprehensive. I will discuss it in detail later.

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group


Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

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.