获取nsdictionary中特定键的值

时间:2022-02-12 13:45:54

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

我将json解析结果保存在如下的字典中:

{
 "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".

现在我该怎么处理第一个索引中的值(类别,标题,id)“abcd”。

If anyone has any knowledge please look upon that.

如果有人有任何知识,请查阅。

Thanks to all.

感谢所有。

3 个解决方案

#1


44  

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


3  

Check like this:

检查如下:

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

#3


0  

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

这是一个数组,使用objectAtIndex:(例如[getcat objectAtIndex:0]或getcat[0])

#1


44  

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


3  

Check like this:

检查如下:

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

#3


0  

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

这是一个数组,使用objectAtIndex:(例如[getcat objectAtIndex:0]或getcat[0])