< a >json of basic knowledge
- 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
- data structures for JSON :
- 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.
- An ordered set of data structures that may have nsarray,vector, arrays, and sequences in different languages
- 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
+ JSONObjectWithData:options:error: This method is responsible for converting the JSON data contained in the developed NSData to obj-c objects
+ JSONObjectWithStream:options:error: This method is responsible for converting the JSON data contained in the developed input stream into a Obj-c object
+ DataWithJSONObject:options:error: This method is responsible for converting the developed JSON object into a NSData object
- + 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
- + 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
- 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