N Network
JSON and Xmlnios Academy n Li Mingjie njsonn What is Jsonpjson is a lightweight data format that is typically used for data interaction p server returns data to the client, Typically in JSON format or XML format (except for file downloads) The Pnjson format is much like the dictionary and array in OC
{"Name": "Jack", "Age": 10}
{"Names": ["Jack", "Rose", "Jim"]}
P Standard JSON format NOTE: Key must use double quotation mark PN to dig out specific data from JSON, to parse JSON Pjson convert to OC data type NJSON–OC conversion table NJSON–OC conversion Exercise Njson Resolution N in iOS, Common parsing scenarios for JSON are 4 kinds of p third-party frameworks: Jsonkit, Sbjson, Touchjson (performance from left to right, worse) P Apple Native (comes with): Nsjsonserialization (Best performance) Pnnsjsonserialization common methods Pjson data Àoc objects
+ (ID) Jsonobjectwithdata: (nsdata*) Data options: (nsjsonreadingoptions) Opterror: (nserror**) error;
POC Object Àjson Data
+ (nsdata*) Datawithjsonobject: (ID) objoptions: (nsjsonwritingoptions) Opterror: (nserror**) error;
N parsing the jsonnxmln from the server what is XMLP full name is Extensible Markup Language, translated "Extensible Markup Language" p, like JSON, is also commonly used as an interactive data format p is generally also called an XML document (XML Document) Pnxml Example
<videos>
<videoname= "Little Yellow No. 01" length= "/>
<videoname= "Small yellow No. 02" length= "/>"
<videoname= "Small yellow No. 03" length= "/>
</videos>
Nxml syntax n A common XML document typically consists of the following parts p document declaration p Element (element) p attribute (Attribute) Nxml Syntax – Document declaration n at the very front of the XML document, you must write a document declaration that declares the simplest declaration of type P for an XML document
<?xml version= "1.0"?>
P Use the Encoding property to describe the character encoding of the document
<?xml version= "1.0" encoding= "UTF-8"?>
Nxml syntax – element n An element consists of a start tag and an end tag p having element content:<video> </video>p no element content:<video></video> P no element content shorthand:<video/> pn An element can nest several child elements (no cross-nesting)
<videos>
<video>
<name> Small Yellow No. 01 Department </name>
<length>30</length>
</video>
</videos>
N Canonical XML documents have a maximum of 1 root elements, and other elements are descendants of the root element
Nxml Syntax – Note that all spaces and line breaks in the nxml are treated as concrete content n the contents of the following two elements are different p 1th
<video> Small yellow people </video>
P 2nd
<video>
Minions
</video>
Nxml Syntax – attribute (Attribute) n An element can have multiple properties
<video name= "Little yellow No. 01" length= "/>"
Pvideo element has name and length two attributes p attribute value must be enclosed in double quotation marks "" or single quotation mark "in the fact that the information represented by a property can also be represented by a child element, such as
<video>
<name> Small Yellow No. 01 Department </name>
<length>30</length>
</video>
Nxml parsing N to extract useful information from XML, you must learn to parse XMLP extract the contents of the name element
<name> Small Yellow No. 01 Department </name>
P Extract the value of the name and length property in the video element
<video name= "Little yellow No. 01" length= "/>"
Nxml has 2 types of pdom: one-time loading the entire XML document into memory, which is more suitable for parsing small files Psax: Starting from the root element, in order to parse an element down, it is more suitable for parsing large files
XML parsing in nios N in iOS, parsing XML means there are many p Apple native ünsxmlparser:sax way parsing, using simple üp third-party framework ÜLIBXML2: pure C language, by default included in the iOS SDK, Support DOM and Sax parsing ügdataxml:dom mode parsing, developed by Google, based on LIBXML2
Nxml choice of analytic mode P large file: Nsxmlparser, libxml2p Small file: Gdataxmlnnsxmlparsern use steps
Incoming XML data, creating a parser
Nsxmlparser*parser = [[Nsxmlparser alloc] initwithdata:data];
Set up the agent to listen to the parsing process
Parser.delegate = self;
Start parsing
[Parser parse];
Nnsxmlparser take is the SAX parsing, the feature is event-driven, the following situation will notify the agent p when scanning to the beginning and end of the document (documents) when scanning to the beginning and end of the elements (element)
Nnsxmlparserdelegaten called when scanning to the beginning of a document (parsing is started)
-(void) Parserdidstartdocument: (nsxmlparser*) Parser
n is called when scanning to the end of the document (parsing is complete)
-(void) Parserdidenddocument: (nsxmlparser*) Parser
nn called when scanning to the beginning of an element (Attributedict holds the attributes of the element)
-(void) parser: (nsxmlparser*) Parser didstartelement: (nsstring*) elementname NamespaceURI: (nsstring*) NamespaceURI QualifiedName: (nsstring*) QName attributes: (nsdictionary*) attributedict
nn called when scanning to the end of an element
-(void) parser: (nsxmlparser*) Parser didendelement: (nsstring*) elementname NamespaceURI: (nsstring*) NamespaceURI QualifiedName: (nsstring*) QName
Nngdataxml configuration ngdataxml based on LIBXML2 Library, you have to do the following configuration p import libxml2 library PPPP
P Set LIBXML2 header file search path (in order to find all header files of the LIBXML2 library) ü Add/USR/INCLUDE/LIBXML2 to head SearchPath
P Set link parameters (Auto link libxml2 library) ü Add-LXML2 to other Linker flags
Ngdataxml configuration n Because Gdataxml is non-arc, So you have to set the compilation parameters Ngdataxml use the class pgdataxmldocument commonly used in Ngdataxml: representing the entire XML document Ppgdataxmlelementü representing each element in the document ü use Attributeforname: Method can get the property value Pnjson and XML compare n the same data, can be expressed either in JSON or in XML to represent n
JSON and XML