json - How to rectify this error class is not key value coding in ios? -


i need iterate "c"id,"project" id,name in following json response:

    {       "a": {         "user": {           "id": 1,           "name": "b"         },         "startday": "2015-12-27",         "status": "new",         "total": 6,         "c": [           {           "id": 768,           "project": {             "id": 8,             "name": "d"           },           "user": {             "id": 1,             "name": "b"           },           "activity": {             "id": 8,             "name": "e"           },           "hours": 2,           "comments": "",           "spent_on": "2015-12-27"         },         {           "id": 775,           "project": {             "id": 8,             "name": "d"         },         "user": {           "id": 1,           "name": "b"         },         "activity": {           "id": 8,           "name": "e"         },         "hours": 4,         "comments": "",         "spent_on": "2015-12-28"        }      ]    }  } 

i've tried

id jsonobjects = [nsjsonserialization jsonobjectwithdata:jsonsource options:nsjsonreadingmutablecontainerserror:nil]; nslog(@"%@",jsonobjects); if ([jsonobjects objectforkey:@"a"] != [nsnull null]) {   nsarray *itemarray = [jsonobjects objectforkey:@"a"];   (nsdictionary *itemdic in itemarray){     nsstring * user;     nsarray *user_data =[itemdic valueforkey:@"c"];     user = [user_data valueforkey:@"id"];     nslog(@"%@",user);   } } 

which throws error

this class not key value coding complaint key c. 

then tried

nsdictionary *dictionary =[nsjsonserializationjsonobjectwithdata:jsonsource  options:kniloptions error:&error]; nsarray * time_entrid = [dictionary valueforkeypath:@"a.c.id"]; nslog(@"%@",time_entrid); 

how can solve error i've mentioned above , iterate properly? using second way enable me iterate too?

you have 1 nsarray , nsdictionary "a" nsdictionary use (you can't iterate nsdictionary way is)

[[[dictionary objectforkey:@"a"] objectforkey:@"user"] objectforkey:@"id"] 

you 1, change id name b

for nsarray

[[[[dictionary objectforkey:@"a"] objectforkey:@"c"] objectatindex:0] objectforkey:@"id"] 

you id 768

for project id

[[[[[dictionary objectforkey:@"a"] objectforkey:@"c"] objectatindex:0] objectforkey:@"project"] objectforkey:@"id"] 

you 8

[[[[[dictionary objectforkey:@"a"] objectforkey:@"c"] objectatindex:0] objectforkey:@"project"] objectforkey:@"name"] 

you name

to iterate array use

for(int i=0; i<[[[dictionary objectforkey:@"a"] objectforkey:@"c"] count];i++) {  nslog(@"%@", [[[[dictionary objectforkey:@"a"] objectforkey:@"c"] objectatindex:i] objectforkey:@"id"]); } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -