OBJECTIVE-C, Ios,iphone Development Basics: JSON parsing (using Apple's official JSON library: nsjsonserialization) __json

Source: Internet
Author: User

The popularity of JSON and XML personally felt to simplify the difficulty of reading, as well as reduce network load, JSON and XML data format after the format is a tree-like structure, you can tree vine touch melon to get any fruit you want.

And when unformatted, JSON and XML are an ordinary string that only needs to be requested once in a network communication, rather than a request server or target host that repeats every time a value is obtained for the wood.

Both JSON and XML store data in the form of key-value.

XML use: < key > Value </key >

JSON uses: "Key": "Value"

Apple offers an official JSON parsing library nsjsonserialization

Nsjsonserialization contains two methods for parsing JSON data in different data formats.

1, + (ID) jsonobjectwithdata: (NSData *) Data options: (nsjsonreadingoptions) opt error: (NSERROR * *) error; Using buffer data to parse

2, + (ID) Jsonobjectwithstream: (Nsinputstream *) stream options: (nsjsonreadingoptions) opt error: (NSERROR * *) error; Parsing JSON using the form of a file stream

The steps to parse JSON are probably to read the JSON string to the buffer and then parse it using the method inside the nsjsonserialization, which is different from the type of data that might be returned in the JSON format, so it's best to use an ID type.

Eg:id dic = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutableleaves Error:nil];

When you get the parsed data, then step by step, one key-value to find the one you want.

Eg 1: Read the JSON data from the local file and read to the buffer.


-(void) jsonparse{//initialization of the file path.
   nsstring* path = [[NSBundle mainbundle] pathforresource:@ "nanjing" oftype:@ "TXT"]; Read the contents of the file into the string, pay attention to encoding nsutf8stringencoding prevent garbled, nsstring* jsonstring = [[NSString alloc] Initwithcontentsoffile:path enc
   Oding:nsutf8stringencoding Error:nil];
    Writes a string to a buffer.
    nsdata* jsondata = [jsonstring datausingencoding:nsutf8stringencoding]; Parsing JSON data, using system methods jsonobjectwithdata:options:error:nsdictionary* dic = [Nsjsonserialization jsonobjectwithdata:jso
    Ndata options:nsjsonreadingmutableleaves Error:nil]; The next step is to parse.
    Know to get what you want. 
    nsarray* arrayresult =[dic objectforkey:@ "Results"];
    nsdictionary* resultdic = [Arrayresult objectatindex:0];
    nsdictionary* geometrydic = [resultdic objectforkey:@ "geometry"];
    NSLog (@ "Geometrydic:%@, resultdic:%@", geometrydic,resultdic);
    nsdictionary* locationdic = [geometrydic objectforkey:@ "Location"];
    nsnumber* lat = [locationdic objectforkey:@ "lat"]; nsnumber* LNG = [LocatiOndic objectforkey:@ "LNG"];
    NSLog (@ "lat =%@, LNG =%@", lat,lng);
    
    
[Jsonstring release]; }

Eg 2: Using the network path to parse JSON,

-(void) jsonparse{//initialization of the network path.
    nsstring* path = @ "Http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
   Initialize URL nsurl* url = [Nsurl Urlwithstring:path]; Read the contents of the file into the string, pay attention to encoding nsutf8stringencoding to prevent garbled, nsstring* jsonstring = [[NSString alloc]initwithcontentsofurl:url Encod
   Ing:nsutf8stringencoding Error:nil];
    Writes a string to a buffer.
    nsdata* jsondata = [jsonstring datausingencoding:nsutf8stringencoding]; Parsing JSON data, using system methods jsonobjectwithdata:options:error:nsdictionary* dic = [Nsjsonserialization jsonobjectwithdata:jso
    
    Ndata options:nsjsonreadingmutableleaves Error:nil]; 
    For a custom resolution, I want to do how to dry nsarray* arrayresult =[dic objectforkey:@ "Results"];
    nsdictionary* resultdic = [Arrayresult objectatindex:0];
    nsdictionary* geometrydic = [resultdic objectforkey:@ "geometry"];
    NSLog (@ "Geometrydic:%@, resultdic:%@", geometrydic,resultdic); nsdictionary* locationdic = [geometrydic objectforkey:@ "Location"];
    nsnumber* lat = [locationdic objectforkey:@ "lat"];
    nsnumber* LNG = [locationdic objectforkey:@ "LNG"];
    NSLog (@ "lat =%@, LNG =%@", lat,lng);
        
[Jsonstring release]; }


Eg 3: Use the network path to parse JSON. Request network data using Nsurlrequest and nsurlconnection.

-(void) jsonparse{//initialization of the network path.
    nsstring* path = @ "Http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
    Initialize URL nsurl* url = [Nsurl Urlwithstring:path];
    nsurlrequest* request = [Nsurlrequest Requestwithurl:url];
    Writes the requested string to a buffer.
    nsdata* jsondata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; Parsing JSON data, using system methods jsonobjectwithdata:options:error:nsdictionary* dic = [Nsjsonserialization jsonobjectwithdata:jso
    
    Ndata options:nsjsonreadingmutableleaves Error:nil]; 
    For a custom resolution, I want to do how to dry nsarray* arrayresult =[dic objectforkey:@ "Results"];
    nsdictionary* resultdic = [Arrayresult objectatindex:0];
    nsdictionary* geometrydic = [resultdic objectforkey:@ "geometry"];
    NSLog (@ "Geometrydic:%@, resultdic:%@", geometrydic,resultdic);
    nsdictionary* locationdic = [geometrydic objectforkey:@ "Location"];
    nsnumber* lat = [locationdic objectforkey:@ "lat"]; Nsnumber* LNG = [locationdic objectforkey:@ "LNG"];
        
NSLog (@ "lat =%@, LNG =%@", lat,lng); }

Demo Download: http://download.csdn.net/download/wsq724439564/6207829

Related Article

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.