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