Here is a new Jsonparser class, create a new method, then parse it in the method, and then execute the parsing method in the root attempt.
Mainviewcontroller.m
#import "MainViewController.h" #import "XMLSAXParser.h" #import "JSONParser.h" @interface mainviewcontroller () @end @implementation mainviewcontroller- (ID) initwithnibname: (NSString *) Nibnameornil bundle: (nsbundle *) nibbundleornil{ self = [ super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // Custom initialization } return self;} - (void) viewdidload{ [super viewdidload]; // do any additional setup after loading the view. self.view.backgroundcolor = [uicolor cyancolor]; //starting the JSON method jsonparser *json = [[jsonparser alloc] init]; [json startjsonparser]; [json release]; }- (void) didreceivememorywarning{ [super didreceivememorywarning]; // dispose of any resources that can be recreated.} /* #pragma mark - navigation// in a storyboard-based application, you will often want to do a little preparation before navigation- (void) Prepareforsegue: (uistoryboardsegue *) Segue sender: (ID) sender{ // Get the new view controller using [segue Destinationviewcontroller]. // pass the selected object to the new view controller.} */@end
JSONParser.h
#import <Foundation/Foundation.h> @interface Jsonparser:nsobject//json Method-(void) startjsonparser; @end
Jsonparser.m
#import "JSONParser.h" #import "Student.h" @implementation JSONParser- (void) Startjsonparser { //system provides the amount of JSON parsing method nsstring *strpath = [[nsbundle mainbundle] pathforresource:@ "Student" oftype:@ "TXT"]; NSData *data = [NSData dataWithContentsOfFile:strPath]; //Parameter 1: File data path information //parameter 2: Set resolved result //parameter 3 : Error message nserror *error = nil; nsarray *array = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error : &error]; nslog (@ "%@", array); for (Nsdictionary *dic in array) { student *stu = [[student alloc] init]; stu.name = [ dic objectforkey:@ "Name"]; stu.sex = [dic objectforkey:@ "Sex"]; stu.phone = [dic objectforkey:@ "Phone"]; stu.number = [dic objectforkey:@ "number"];&NBSP;&NBSP;&NBSP;&NBSP;}} @end
This article is from "Xiao Liu _blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1554577
JSON parses the data.