IPhone ios json parsing process, tutorial

Source: Internet
Author: User

The following are some of your friendly support:

Made a product, need popularity support a bit, Android and iPhone 91 market search # super junior mission #, or directly to the page to download the http://m.ixingji.com/m.html? P = x16

JSONIs the current popular data format, andXMLCompared,JSONMore clever. When the metadata is the same,JSONIn most casesXMLThe transfer case is small.JSONPerformance ComparisonXMLFast.

InIPhone cocoa touch frameworkNot inLibraryProceedJSONData format. Fortunately, someObjective CToken is enabled.JSONData formatLibrary.
And I am using this: JSON-framework
Wait until the next job is completed.JSONThe amount of data is sentProjectAnd thenClassTo useJSON LibraryAs longJSON. hYou can.

{    "glossary": {        "title": "example glossary",        "GlossDiv": {            "title": "S",            "GlossList": {                "GlossEntry": {                    "ID": "SGML",                    "SortAs": "SGML",                    "GlossTerm": "Standard Generalized Markup Language",                    "Acronym": "SGML",                    "Abbrev": "ISO 8879:1986",                    "GlossDef": {                        "para": "A meta-markup language, used to create markup languages such as DocBook.",                        "GlossSeeAlso": ["GML", "XML"]                    },                    "GlossSee": "markup"                }            }        }    }}

The above information will be stored in oneData. JSON.
Currently, this example is used:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];    NSString *jsonStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];              SBJsonParser *parser = [[SBJsonParser alloc] init];    NSDictionary *json = [parser objectWithString:jsonStr error:nil];         NSDictionary *glossary = [json objectForKey:@"glossary"];    NSString *glossaryTitle = [glossary objectForKey:@"title"];         NSDictionary *glossDiv = [glossary objectForKey:@"GlossDiv"];    NSString *glossDivTitle = [glossDiv objectForKey:@"title"];         NSArray *glossSeeAlso = [[[[glossDiv objectForKey:@"GlossList"]                                objectForKey: @"GlossEntry"]                                     objectForKey: @"GlossDef"]                                         objectForKey: @"GlossSeeAlso"];         NSLog(@"Glossary Title: %@", glossaryTitle);    NSLog(@"GlossDiv Title : %@", glossDivTitle);         NSLog(@"GlossSeeAlso item 1: %@", [glossSeeAlso objectAtIndex:0]);    NSLog(@"GlossSeeAlso item 2: %@", [glossSeeAlso objectAtIndex:1]);

Row 3 is to include the case inNsstring.
CreateJSON parser, Too soonJSONString successObject.
Line 2 willJSONString successNsdictionaryAnd thisNsdictionaryInternalObjectHas beenJSON parserParse into the corresponding object, for example:Nsarray,Nsdictionary,NsstringAnd so on.
The fourth line uses the word ""."Glossary"ExtractionObjectBecauseObjectIs the information that includes the following parts:

{        "title": "example glossary",        "GlossDiv": {            "title": "S",            "GlossList": {                "GlossEntry": {                    "ID": "SGML",                    "SortAs": "SGML",                    "GlossTerm": "Standard Generalized Markup Language",                    "Acronym": "SGML",                    "Abbrev": "ISO 8879:1986",                    "GlossDef": {                        "para": "A meta-markup language, used to create markup languages such as DocBook.",                        "GlossSeeAlso": ["GML", "XML"]                    },                    "GlossSee": "markup"                }            }        }    }

The first row uses the above information plus the relevant words."Title"Extract content, and add the words related to the above information"Title"Content is"Example glossary", SoObjectIsNsstring
Object
.
14th rows are extracted"Glossseealso"As follows:

["GML", "XML"]

As you can see, this isArray, SoObjectIsNsarray object.

The following content will be printed at the end:

2010-08-27 15:15:44.003 JSON[47655:207] Glossary Title: example glossary2010-08-27 15:15:44.049 JSON[47655:207] GlossDiv Title : S2010-08-27 15:15:44.050 JSON[47655:207] GlossSeeAlso item 1: GML2010-08-27 15:15:44.050 JSON[47655:207] GlossSeeAlso item 2: XML

IfJSONResources are retrieved by the network. You can use the following methods:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://pro.ctlok.com/data.json"]];NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];NSString *jsonStr = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

Example: json.zip

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.