JSON parses the data.

Source: Internet
Author: User

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.

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.