ios - JSON Serialization Returns Null -
in our project taking values json services
most questions asked many times not find solution.
here code block:
nsdata *urldata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nsstring *responsedata = [[nsstring alloc]initwithdata:urldata encoding:nsutf8stringencoding]; nslog(@"response ==> %@", responsedata); nserror *error = nil; nsdictionary *jsondata = [nsjsonserialization jsonobjectwithdata:urldata options:nsjsonreadingmutablecontainers error:&error]; nsstring* userid = [jsondata objectforkey:@"user_id"]; nslog(@"success: %ld userid: %ld",(long)success, (long)userid);
log:
response code: 200
response ==>
{"user_id":4}
success: 0 userid: 0
as can see want convert json object string when printout content of jsondata returns null guess have issue jsonserializaton. can suggest solution?
thanks, umit
just test following code, it's ok. should log error , check response data, may contain invalid encoding.
const char *jsonstr = "{\"user_id\":4}\n"; nsdata *jsondata = [nsdata datawithbytes:jsonstr length:strlen(jsonstr)]; nserror *error = null; nsdictionary *jsonobj = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&error]; nsnumber *useridobj = [jsonobj objectforkey:@"user_id"]; nsinteger userid = [useridobj integervalue]; nslog(@"=========>user id: %d", userid);
Comments
Post a Comment