Wednesday, 15 August 2012

ios - Parsing JSON response -



ios - Parsing JSON response -

i using afjsonrequestoperation request remote api:

nsurlrequest *request = [nsurlrequest requestwithurl:url]; afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { //remove svprogresshud view [svprogresshud dismiss]; //check value returned server nsdata *jsondata = [json datausingencoding:nsutf8stringencoding];//this line cause crash nsarray *arr = [nsjsonserialization jsonobjectwithdata:jsondata options:0 error:nil]; logindic=[[nsdictionary alloc]init]; logindic=[arr objectatindex:0]; nslog(@"%@",logindic); } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { nslog(@"request failed error: %@", [error.userinfo objectforkey:@"nslocalizeddescription"]); }]; [operation start]; [svprogresshud showwithstatus:@"loading"];

however, app crashes , getting error:

[__nscfdictionary datausingencoding:]: unrecognized selector sent instance

here nslog json object returned:

result = ( { operation = 5; result = 1; } );

am missing something, because think not parsing correctly json object. please right me.

it looks afjsonrequestoperation deserializing json dictionary you, , you're trying again. json nsdictionary you're calling nsstring method.

remove of code:

nsdata *jsondata = [json datausingencoding:nsutf8stringencoding];//this line cause crash nsarray *arr = [nsjsonserialization jsonobjectwithdata:jsondata options:0 error:nil]; logindic=[[nsdictionary alloc]init]; logindic=[arr objectatindex:0];

and replace with:

logindic = [[json objectforkey:@"result"] lastobject];

(that'll work safely without checking array bounds, assumes there's 1 element in array.)

ios json afnetworking

No comments:

Post a Comment