1 #import "ViewController.h"2 #import "Student.h"3 #import "GDataXMLNode.h"4 #import "JSONKit.h"5 6 @interfaceViewcontroller () <NSXMLParserDelegate>7 8 /**9 * An array of stored dataTen */ One@property (nonatomic, strong) Nsmutablearray *DataArray; A - @end - the @implementationViewcontroller - -- (void) Viewdidload { - [Super Viewdidload]; + //additional setup after loading the view, typically from a nib. - } + A #pragmaMark-JSON data parsing from the system at-(Ibaction) Foundationparseractionjsondocument: (UIButton *) Sender { - - //1. Get the file path -NSString *path = [[NSBundle mainbundle] Pathforresource:@"Studentinfo_json.txt"Oftype:nil]; - - in //2. Get NSData by Path -NSData *data =[NSData Datawithcontentsoffile:path]; to + - //3. Initialize an array of stored data theSelf.dataarray =[Nsmutablearray array]; * $ Panax Notoginseng //4. Start the parsing -Nsarray *resultarray =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; the + A //5. Iterate through the array and assign a value to the object using KVC the for(Nsdictionary *dictinchresultarray) { + -Student *stu =[[Student alloc] init]; $ $ //assigns a value from an array to an object - [Stu Setvaluesforkeyswithdictionary:dict]; - the //to add an object to an array - [Self.dataarray Addobject:stu];Wuyi } the - Wu //Traverse Inspection - for(Student *stuinchSelf.dataarray) { AboutNSLog (@"name =%@, gender =%@, age =%ld, hobby =%@", Stu.name, Stu.gender, Stu.age, stu.hobby); $ } - - } - A @end
Nsjsonserialization parsing json file with system's own