ios - How we get data from server using the JSON query? -
**in code when add breakpoint viewdidload( ) function got greetingarray.count 0 when add breakpoint @ loop works , got results 3 values of greetingarray. possible reason no getting data server.there no problem server side.i check it.
- (void)viewdidload { [super viewdidload]; greetingarray = [[nsmutablearray alloc] init]; greetingdictionary = [[nsmutabledictionary alloc] init]; nsstring *connectionstring; connectionstring=[nsstring stringwithformat:@"http://xxx.xxx.x.xx/testmgt/api/%@",self.fieldname]; nsurl *url = [nsurl urlwithstring:connectionstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { if (data.length > 0 && connectionerror == nil) { nslog(@"----------------------------------------------------"); nslog(@"data length = %d",data.length); greetingmarray = [nsjsonserialization jsonobjectwithdata:data options:0 error:null]; nslog(@"%@",greetingmarray); for(int = 0 ; i< greetingmarray.count; i++) { greetingdictionary = (nsmutabledictionary *)[greetingmarray objectatindex:i]; nslog(@"%@",greetingdictionary); connectionovertime *overtime = [[connectionovertime alloc] init]; overtime.entrydate=[greetingdictionary valueforkey:@"entrydate"]; [greetingarray addobject:overtime]; nslog(@"%d",greetingarray.count); } } }]; }
if don't answer try jsonframework library , import sbjsonparser.h example try below code - (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. self.chapterid=[[nsmutablearray alloc]init]; self.chaptername=[[nsmutablearray alloc]init]; nsurl *url=[nsurl urlwithstring:@"https://www.coursekart.com/webservice/load-subjects.php?api_key=68410920ghjflac878&standard_id=2&format=json"]; nsurlrequest *request=[[nsurlrequest alloc]initwithurl:url]; nserror *error; nsurlresponse *response; nsdata *data=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; if(data!=nil) { nsstring *content=[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; if(content.length!=0) { sbjsonparser *parser=[[sbjsonparser alloc]init]; nsarray *dir=[[parser objectwithstring:content]objectforkey:@"subjectlist"]; for(int i=0;i<dir.count;i++) { nsdictionary *array=[dir objectatindex:i]; nsarray *data=[array objectforkey:@"data"]; nsdictionary *dat=(nsdictionary *)data; nsstring *idch=[dat objectforkey:@"id"]; nsstring *slug=[dat objectforkey:@"slug"]; [chapterid addobject:idch]; [chaptername addobject:slug]; // nslog(@"%@",[chapterid objectatindex:0]); //nslog(@"%@",[chaptername objectatindex:0]); } } } }
Comments
Post a Comment