XML, JSON

Source: Internet
Author: User

1. JSON

JSON is the most widely used data format for transmitting data on the network; JSON is a subset of JavaScript and is specifically responsible for describing the data format (JavaScript is a "scripting" language for Web development, and JavaScript has nothing to do with Java!). )

More lightweight than XML

1.1 Syntax rules

(1) Data expressed as key-value pairs (key/value)

(2) data separated by commas

(3) Curly brackets Save the object

(4) square brackets Save array

The syntax format of a JSON description object is much like a dictionary, but not a dictionary, just a form of a key-value pair;

JSON represents a single object:(there is @ inthe dictionary, there is no @ in JSON )

1.2 JSON value

(1) Number (integer, floating point type)

(2) string

(3) Logical value (TRUE or FALSE)

(4) Arrays, objects

(5) NULL

Serialization: The NSArray NSDictionary process of converting/translating to binary before sending data to the server

Deserialization: After data is received from the server, the binary data is converted to NSArray /NSDictionary

1.3 Parsing of data (deserialization)

The data obtained from the server is binary data, the client cannot be used directly, and the binary data needs to be converted to the OC object that the client can use directly

(1) Apple native

Nsjsonserialization class

ID result = [nsjsonserialization JSONObjectWithData:options:error:];

NSLog (@ "%@", [result class]);

<1> If the result of printing JSON type is _nscfdictionary, the dictionary is represented (immutable);

Nsjsonserialization automatically parses a binary form of a JSON string into a dictionary or array;

The object returned by JSON parsing is a dictionary or an array, depending on whether the original data is enclosed in {} or [] .

<2>optins Options

Nsjsonreadingmutablecontainers = (1UL << 0)---> Container variable

Nsjsonreadingmutableleaves = (1UL << 1)---> Leaf variable

Nsjsonreadingallowfragments = (1UL << 2)---> Top node may not be Nsarray or nsdictionary

We usually fill in the options parameter 0(the default is immutable), indicating that any additional operations are not done, the most efficient!

This error indicates that the JSON file is wrong:

(2) Jsonkit (MRC)

JsonkIt has stopped updating for IOS 5.0 ( native at the beginning of 5.0 ) with the previous version development using

Jsonkit Deserialization Core Code:

ID result = [[Jsondecoder decoder] objectwithdata:data];

2. XML (Extensible Markup Language)

XML is designed to transmit and store data; there is only one root node; the start node must correspond to the end node.

2.1 Parsing methods

(1) DOM

Is the parsing method used in MAC; the memory consumption is very high, not suitable for the mobile phone; the iphone cannot parse XML directly using the DOM method

(2) SAX

is a read-only way to parse from top to bottom; it is an analytic way provided by Apple; Nsxmlparser through the realization of the resolution 代理 ; fast

Resolution steps:

    1. Start document-Prepare for work
    2. Start "Node"
    3. Discover the contents of the node, each node, may take several times to find out
    4. End "Node"
    5. End Document-Parse end

Example:

-(void) Viewdidload {

[Super Viewdidload];

1.URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://192.168.34.36/videos.xml"];

2.NSURLRequest

Nsurlrequest *request = [nsurlrequest requestwithurl:url cachepolicy:0 timeoutinterval:30];

3. Make a connection, send a request

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror * _nullable connectionerror) {

4. Get the response data from the server for processing

if (Connectionerror==nil &&data!=nil &&data.length>0) {

1.xml you need to start with an XML parser.

Nsxmlparser *parser = [[Nsxmlparser alloc] initwithdata:data];

2. Set up proxy for our parser

Parser.delegate = self;

3. Start parsing

[Parser parse];

}

}];

}

Implement the Proxy method and start parsing

Document Start

-(void) Parserdidstartdocument: (Nsxmlparser *) parser{

NSLog (@ "----Start document---");

}

Start a tag

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (Nullable NSString *) NamespaceURI qualifiedname: (Nullable NSString *) QName attributes: (nsdictionary<nsstring *, nsstring *> *) attributedict{

NSLog (@ "Start%@---", elementname);

}

When we parse to the content, put the value that he printed multiple times, stitch the string, and the mutable string

-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{

NSLog (@ "%@", string);

}

End a label

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (Nullable NSString *) NamespaceURI qualifiedname: (Nullable NSString *) qname{

NSLog (@ "End%@---", elementname);

}

End of document

-(void) Parserdidenddocument: (Nsxmlparser *) parser{

NSLog (@ "----End Document---");

}

3. The difference between JSON and XML

(1) The data readability of JSON and XML is basically the same

(2) XML has good extensibility, JSON is also extensible

(3) XML parsing needs to consider the parent node, child nodes; JSON parsing does not need to consider these

(4) There are many cases where JSON parsing is used more

(5) The data volume of JSON is smaller relative to XML

XML, JSON

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.