JSON parsing of network requests

Source: Internet
Author: User
Tags comparison table

< a >json of basic knowledge

  1. What is JSON: The full name of JSON is the JavaScript object Notation (JavaScript objects symbol), which is the most widely used data interchange format, with cross-platform . Cross-language advantages, and control Xml,json as a data transmission format, data transfer is smaller , JSON data format is suitable for people to read and write, but also suitable for the computer itself parsing and synthesis
  2. data structures for JSON :
      1. A data structure composed of Key-value , which has different implementations in different languages, such as: An object in JavaScript, a Nsdictionary object in Objective-c, In the C language, it is a struct, which may correspond to other structures in other languages.
      2.   An ordered set of data structures that may have nsarray,vector, arrays, and sequences in different languages
  3. JSON URL : http://www.json.org

< two >JSON-OC data type comparison table

JSON-OC data table
Json Oc
Curly braces {} Nsdictionary
Brackets [] Nsarray
Double quote "" NSString
Number 1, 2.3 NSNumber

JSON parsing and generation in < three >objective-c: (Json->objective-c Objectivew->json)

After IOS5, the IOS SDK provides the Nsjaonserialization class to parse and generate JSON, but in some cases we choose a third-party library to process the JSON in order to be compatible with previous versions or to seek more powerful functionality . Common third-party libraries are Sbjson, Jsonkit, and have model-handling class libraries such as: Mjextension,jsonmodel,yymodel to help with JSON parsing

< four > Using nsjsonserialization to process JSON data

Nsjsonserailization provides the following classes of methods to support JSON parsing and generation

    1. + JSONObjectWithData:options:error: This method is responsible for converting the JSON data contained in the developed NSData to obj-c objects

    2. + JSONObjectWithStream:options:error: This method is responsible for converting the JSON data contained in the developed input stream into a Obj-c object

    3. + DataWithJSONObject:options:error: This method is responsible for converting the developed JSON object into a NSData object

    4. + WriteJSONObject:toStream:options:error: This method is responsible for converting the prepared JSON object into a JSON string output into the output stream that was developed

Options

Nsjsonreadingmutablecontainers = (1UL << 0), all nodes variable

Nsjsonreadingmutableleaves = (1UL << 1), leaf node variable

Nsjsonreadingallowfragments = (1UL << 2) variable root node

    1.  +  Isvalidjsonobject: This method is used to determine whether a developed object can be converted to JSON data

Since nsjsonserailization cannot convert arbitrary objects to JSON data, a class method Isvalidjsonobject is used to determine whether the object can be converted to JSON data.

Nsjsonserailization can only be converted to JSON objects for objects that meet the following conditions

    • The top-level object can only be Nsarray or nsdictionary
    • The objects contained in the collection can only be nsstring,nsnumber,nsarray,nsdictionary or Nsnull objects
    • All nsdictionary keys can only be nsstring.
    • NSNumber the value of the package cannot be Nan or infinity

< five > Using Jsonkit to process JSON data

Jsonkit's GitHub Address:

Jsonkit provides the following classes of methods to support JSON parsing and generation

    1. Classification of JSON data used to parse the NSString type

@interface NSString (jsonkitdeserializing)

-(ID) objectfromjsonstring;

-(ID) objectfromjsonstringwithparseoptions: (jkparseoptionflags) parseoptionflags;

-(ID) objectfromjsonstringwithparseoptions: (jkparseoptionflags) parseoptionflags error: (NSERROR * *) error;

-(ID) mutableobjectfromjsonstring;

-(ID) mutableobjectfromjsonstringwithparseoptions: (jkparseoptionflags) parseoptionflags;

-(ID) mutableobjectfromjsonstringwithparseoptions: (jkparseoptionflags) parseoptionflags error: (NSERROR * *) error;

@end

2. classification of JSON data for parsing nsdata types

@interface NSData (jsonkitdeserializing)

The NSData must be UTF8 encoded JSON.

-(ID) objectfromjsondata;

-(ID) objectfromjsondatawithparseoptions: (jkparseoptionflags) parseoptionflags;

-(ID) objectfromjsondatawithparseoptions: (jkparseoptionflags) parseoptionflags error: (NSERROR * *) error;

-(ID) mutableobjectfromjsondata;

-(ID) mutableobjectfromjsondatawithparseoptions: (jkparseoptionflags) parseoptionflags;

-(ID) mutableobjectfromjsondatawithparseoptions: (jkparseoptionflags) parseoptionflags error: (NSERROR * *) error;

@end

3. Jsonkit provides nsdictionary,nsarray,nsstring with a way to convert obj-c objects to JSON data

-(NSData *) Jsondata;

-(NSString *) jsonstring;

< six > Using Sbjson to process JSON data

Sbjson's GitHub Address:

JSON parsing of network requests

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.