get values of particular key in nsdictionary

元气小坏坏 提交于 2019-11-28 20:16:34

问题


I saved the json parsing result in a dictionary which look like:

{
 "statusCode":"200",
"body":[
  {
     "status":"success",
     "remarks":null
  }
],

"data":[
     "abcd":[
        {
           "category":"a",
           "title":"b",
           "id":"24"
        },
        {
           "category":"c",
           "title":"crd",
           "id":"65"
        },
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        }

     ]
  },
  {
     "efgh":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

                             ]
  },
  {
     "ijkl":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

     ]
  }
 ]
}

The data for key @"data" can be saved into an array by using

NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];

Now what should I do for the values(category, title, id) inside the first index i.e. "abcd".

If anyone has any knowledge please look upon that.

Thanks to all.


回答1:


Following will give you the desired object

NSDictionary *dict=[results valueForKeyPath:@"data.abcd"][0];

For individual:

NSString *categoryString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"Category"];
NSString *idString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"id"];
NSString *titleString=[[results valueForKeyPath:@"data.abcd"][0] objectForKey:@"title"];

Also,

NSString *categoryString=dict[@"Category"];
NSString *idString=dict[@"id"];
NSString *titleString=dict[@"title"];



回答2:


Check like this:

NSString *value=[[[getcate valueForKey:@"abcd"] objectAtIndex:0] valueForKey:@"category"];



回答3:


That is an array so use objectAtIndex: (i.e. [getcat objectAtIndex:0] or getcat[0])



来源:https://stackoverflow.com/questions/16233425/get-values-of-particular-key-in-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!