JSON:
What is JSON:
1 , JSON is a lightweight data format that is typically used for data interaction;
2, the data returned to the client by the server, usually in JSON format or XML format (except for file download)
The format of JSON is much like the dictionary and array in OC
{"Name": "Jack", "Age": 10}
{"Names": ["Jack", "Rose", "Jim"]}
Note points in the standard JSON format:key must be double-quoted
JSON Parsing scheme:
1, in IOS , there are 4 common parsing methods forJSON
third-party frameworks: Jsonkit, sbjson, Touchjson ( performance from left to right, the worse);
Apple Native (comes with): Nsjsonserialization ( Best performance );
2, Common methods of nsjsonserialization
OC Object , JSON data
[Nsjsonserialization jsonobjectwithdata:<# (NSData *) #> options:<# (nsjsonreadingoptions) #> error:<# ( Nserror *__autoreleasing *) #>];
OC Object --JSON data
[Nsjsonserialization datawithjsonobject:<# (ID) #> options:<# (nsjsonwritingoptions) #> error:<# ( Nserror *__autoreleasing *) #>];
JSON parsing Examples:
- (Nsarray *) Parsejsondata: (NSData *) data
{
parsing Data
Nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil];
Nsarray *array = dict[@ "Videos"];
nsmutablearray *videos = [nsmutablearrayarray];
For (nsdictionary *videodict in array) {
Hmvideo *video = [Hmvideo videowithdict:videodict];
[Videos Addobject:video];
}
return videos;
}
First, XML Introduction : full name is extensible Markup laugage , translated "extensible Markup Language ";
Like JSON , it is also a commonly used data format for interaction;
<videos>
<video name = "abc" length = "/>"
<video name = "ABCD" length = "/>"
<video name = "ABCDE" length = "/>"
</videos>
Second,XML parsing
1,DOM parsing: The whole XML document is loaded into memory at once, it is more suitable for parsing small files; (Random access to a node)
2,SAX parsing: Starting from the root element, in order an element of an element to parse down, more suitable for parsing large files; (no random access to a node)
There are many ways to parse XML in IOS :
1, Apple native
Nsxmlparser:sax method Analysis, easy to use
2, third-party framework
LIBXML2: Pure C language, which is included by default in the IOS SDK while supporting DOM and SAX parsing
Gdataxml:dom method Analysis, developed by Google , based on LIBXML2
3, theXML parsing method suggests:
Small Files : Nsxmlparser,libxml2
Large File : Gdataxml
Nsxmlparser:
Steps to use:
// incoming XML data, create parser;
Nsxmlparser *parser = [[Nsxmlparser alloc] initwithdata:data];
// set agent, monitor parsing process;
Parser.delegate = self;
// start parsing;
[Parser parser];
Nsxmlparser Take is the SAX mode parsing, characterized by event-driven, the following situation will notify the agent;
1, when scanning to the beginning and end of the document
2, when scanning to the beginning and end of the element
Gdataxml Configuration:
1,gdataxml based on the LIBXML2 Library, you have to do the following configuration;
Import the LIBXML2 library,
2, set the libxml2 header File search path (in order to find all header files for libxml2 Library)
in the add/USR/INCLUDE/LIBXML2 in Head Search Path
3, set link parameters (Auto link libxml2)
in the Other Linker Flags added -lxml2
Note: because Gdataxml Non - ARC , so to configure the compilation parameters;
Gdataxml using:
classes commonly used in Gdataxml
Gdataxmldocument: representing the entire XML document
Gdataxmlelement: represents each element in a document
use the Attributeforname: method to get the property value
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Xml/json parsing