>json
is a lightweight data format ( like the document has a txt format , doc format , JSON is a representation of the data format ), Typically used for data interaction ,
Data returned to the client by the server , usually in JSON format or XML format ( except for file downloads , files )
The format of JSON is much like an array in OC , a dictionary . Standard JSON format :key must be in double quotes , not single quotes
To get the specific data from the JSON , you need to parse the JSON . JSON--->oc
>JSON parsing scheme
- ios Json4
- third-party framework: jsonkitsbjsontouchjson
- Apple native (comes with): Nsjsonserialization ( performance best )
- nsjsonserialization common methods
- json data à oc
+ (ID) jsonobjectwithdata: (NSData *) Data options: (nsjsonreadingoptions) opt error: (NSERROR *) error;
+ (NSData *) Datawithjsonobject: (ID) obj options: (nsjsonwritingoptions) opt error: (NSERROR *) error;
Xml
- What is XML
- Full name is Extensible Markup Language, translated "Extensible Markup Language"
- like JSON , it's also a common format for interacting data.
- Generally also called XML document (XML documents)
- XML Example
<videos>
<video Name=" Yellow man first " length=" />"
<video Name=" small Yellow Man", " length=" >
<video Name=" small yellow person " length=" />"
</videos>
XML Syntax
- A common XML document typically consists of the following sections
- Document Declaration
- Elements (element)
- Properties (Attribute)
- XML Syntax – Document declaration
- At the very front of the XML document, you must write a document declaration that declares the type of the XML document
- The simplest statement
<?xml version="1.0" ?>
- Use the encoding property to describe the character encoding of a document
<?xml version= "1.0" encoding= "UTF-8"?>
XML Syntax – elements (element)
- An element includes a start tag and an end tag
- own element content:<video> Yellow people </video>
- No element content:<video></video>
- Shorthand for no element content:<video/>
- An element can nest several child elements (no cross-nesting)
<videos>
<video>
<name> Yellow People section of the department </name>
<length></length>
</video>
</videos>
- Canonical XML documents have a maximum of 1 root elements, and other elements are descendants of the root element
- XML Syntax – element's attention
- all spaces and line breaks in XML are treated as concrete content
- The contents of the following two elements are not the same
- The first 1
<video> Small yellow people </video>
<video>
Minions
</video>
XML Syntax – attributes (Attribute)
- An element can have multiple properties
<video Name=" Yellow man first " length=" />"
- The video element has a name and length two properties
- Attribute values must be enclosed in double quotation marks "" or single quotes "
- In fact, the information represented by a property can also be represented by a child element, such as
<video>
<name> Yellow People section of the department </name>
<length></length>
</video>
XML parsing
- To extract useful information from XML, you have to learn to parse the XML
- Extract The contents of the name element
<name> Yellow People section of the department </name>
- Extract The value of the name and length property in the video element
<video Name=" Yellow man first " length=" /> "
- There are 2 ways to parse XML
- DOM: loading an entire XML document into memory at once, which is more suitable for parsing small files
- SAX: Starting from the root element, in order to parse an element down, it is more suitable for parsing large files
- XML parsing in IOS
- In IOS , there are many ways to parse XML
- Apple native
- Nsxmlparser:SAX method parsing, simple to use
- Third-party frameworks
- LIBXML2: pure C language, which is included by default in the iOS SDK while supporting DOM and SAX parsing
- Gdataxml:DOM mode parsing, developed by Google , based on libxml2
- suggestions for choosing XML parsing methods
- Large files:nsxmlparser,libxml2
- Small files:gdataxml
- Nsxmlparser
- 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];
- Nsxmlparser take is the SAX parsing, characterized by event-driven, the following situation will notify the agent
- When scanning tothe beginning and end of documents (document)
- When scanning tothe start and end of elements (Element)
- Nsxmlparserdelegate
- Called when scanning to the beginning of a document (parsing is started)
-(void) Parserdidstartdocument: (Nsxmlparser *) parser
- Called when scanning to the end of a document (parsing is complete)
-(void) Parserdidenddocument: (Nsxmlparser *) parser
- 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
- Called when scanning to the end of an element
-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qName
Gdataxml Use
- 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
Data parsing--json & XML