objective c - Converting JSON Result as an Array -
i have json data :
array: { data = ( { "com_id" = 1; "com_name" = apple; }, { "com_id" = 2; "com_name" = "google"; }, { "com_id" = 3; "com_name" = "yahoo"; } ); message = "data found"; response = success; }
here's code fetch data :
nsurl * url = [[nsurl alloc] initwithstring:@"https://jsonurlhere.com"]; // prepare request object nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreturncachedataelseload timeoutinterval:30]; // prepare variables json response nsdata *urldata; nsurlresponse *response; nserror *error; // make synchronous request urldata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error]; // construct array around data response nsarray* object = [nsjsonserialization jsonobjectwithdata:urldata options:0 error:&error]; nslog(@"array: %@", object);
now, want use json data pickerview. how change json data array can load replace existing array (self.namecompany
)?
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. self.namecompany = @[@"apple", @"google", @"yahoo"]; }
check code , response not array dictionary.
-(void)loaddata{ nsurl * url = [[nsurl alloc] initwithstring:@"https://jsonurlhere.com"]; // prepare request object nsurlrequest *urlrequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreturncachedataelseload timeoutinterval:30]; // prepare variables json response nsdata *urldata; nsurlresponse *response; nserror *error; // make synchronous request urldata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error]; // construct array around data response nsdictionary* object = [nsjsonserialization jsonobjectwithdata:urldata options:0 error:&error]; nslog(@"array: %@", [object objectforkey:@"data"]); nsmutablearray *companyarray=[[nsmutablearray alloc] init]; (nsdictionary *tempdict in [object objectforkey:@"data"]) { [companyarray addobject:[tempdict objectforkey:@"com_name"]]; } self.namecompany=[nsarray arraywitharray:companyarray]; }
Comments
Post a Comment