这里是新建一个JSONParser类,,先新建一个方法,然后在是在方法里面进行解析,然后在根试图中执行解析方法
# import “MainViewController.h” # import “XMLSAXParser.h” # import “JSONParser.h” @interface MainViewController () @end @implementation MainViewController 安康;(id) initWithNibName:(NSString *) nibNameOrNil 包:nibBundleOrNil (NSBundle *) { ,,,self =, (super initWithNibName: nibNameOrNil 包:nibBundleOrNil]; ,,,if (自我),{ ,,,,,,,//Custom 初始化 ,,,} ,,,return 自我; } 安康;viewDidLoad (void) { ,,,(super viewDidLoad); ,,,//,Do any additional setup after loading 从而视图。 ,,,self.view.backgroundColor =, [UIColor cyanColor]; ,,,//开始执行JSON方法 ,,,JSONParser * json =, [[JSONParser alloc], init); ,,,(json startJSONParser); ,,,(json 释放); ,,,, ,,,, ,,,, ,,,, } 安康;didReceiveMemoryWarning (void) { ,,,(super didReceiveMemoryWarning); ,,,//,Dispose of any resources that 还要be 重新创建。 }/* # pragma mark 安康;导航//,拷贝a storyboard-based 应用程序,,你will often want 用do a little preparation before 导航 安康;(空白)prepareForSegue:(UIStoryboardSegue *) segue 发送方:发送者(id) { ,,,//,Get 从而new view controller using [segue destinationViewController]。 ,,,//,Pass 从而selected object 用,new view 控制器。 } */@end
# import & lt;基?Foundation.h> @interface JSONParser : NSObject//json方法 安康;(void) startJSONParser; @end
# import “JSONParser.h” # import “Student.h” @implementation JSONParser 安康;startJSONParser (void) { ,,,//系统提供额JSON解析方法 ,,,, ,,,NSString * strPath =, [[NSBundle mainBundle], pathForResource: @“学生”,减低:@“三种”); ,,,NSData * data =, [NSData dataWithContentsOfFile strPath):; ,,,, ,,,//参数1:文件数据路径信息 ,,,//参数2:设置解析的结果 ,,,//参数3:错误信息 ,,,NSError * error =,零; ,,NSArray * array =, (NSJSONSerialization JSONObjectWithData: data 选择:NSJSONReadingMutableContainers 错误:和错误); ,,,NSLog(@ % @,,数组); ,,,, ,,,for (NSDictionary * dic 数组拷贝),{ ,,,,,,,Student * stu =, [[Student alloc], init); ,,,,,,,stu.name =, (dic objectForKey: @“name”); ,,,,,,,stu.sex =, (dic objectForKey: @“性”); ,,,,,,,stu.phone =, (dic objectForKey: @“电话”); ,,,,,,,stu.number =, (dic objectForKey: @ number); ,,,} } @end