IOS JSON Data parsing

Source: Internet
Author: User

JSON is a more commonly used data format and is clearer than the XML hierarchy, and here are two ways to parse JSON: Nsjsonserialization and Jsonkit

Nsjsonserialization is an easy-to-use JSON parsing package that is available after IOS 5.

The JSON data format is represented by the corresponding ' [', '] ' and ' {', '} ', which represent the array, which represents the dictionary.

Nsjsonserialization parsing process:

1. Get the file path

2. Get the contents of a file

3. Parsing

Simple Small Example:

1-(Ibaction) Parserjson: (ID) Sender {2     3     //Get file path4     5NSString *jsonpath = [[NSBundle mainbundle] Pathforresource:@"Student"OfType:@"JSON"];6Nserror *error =Nil;7NSData *jsondata =[NSData Datawithcontentsoffile:jsonpath];8Nsmutablearray *array = [Nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&ERROR];9     if(Error = =Nil) {TenNSLog (@"%@", array); One}Else { ANSLog (@"%@", error); -     } -      the     //Data Encapsulation -      -Nsmutablearray *arr =[Nsmutablearray array]; -      +      for(Nsdictionary *dicinchArray) { -Student *stu =[[Student alloc]initwithdictionary:dic]; + [arr Addobject:stu]; A     } at      -      for(Student *stuincharr) { -NSLog (@"%@", Stu); -     } -}

Jsonkit parsing: (code)

1-(Ibaction) Parserjsonwithjesonkit: (ID) Sender {2     3     //Get file path4     5NSString *jsonpath = [[NSBundle mainbundle] Pathforresource:@"Student"OfType:@"JSON"];6Nserror *error =Nil;7NSString *jsonstr = [[NSString alloc]initwithcontentsoffile:jsonpath encoding:nsutf8stringencoding error:&ERROR];8NSLog (@"%@", jsonstr);9     //let Jesonkit parse JSON dataTenNsmutablearray *array =[Jsonstr objectfromjsonstring]; OneNSLog (@"%ld", array.count); A     //Data Encapsulation -Nsmutablearray *arr =[Nsmutablearray array]; -      the      for(Nsdictionary *dicinchArray) { -Student *stu =[[Student alloc]initwithdictionary:dic]; - [arr Addobject:stu]; -     } +      -      for(Student *stuincharr) { +NSLog (@"%@", Stu); A     } at}

IOS JSON Data parsing

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.