获取nsdictionary中特定键的值

时间:2013-04-26 09:43:43

标签: iphone ios objective-c nsdictionary

我将json解析结果保存在dictionary中,如下所示:

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

     ]
  }
 ]
}

密钥@"数据"的数据可以使用

保存到数组中
NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];

现在我应该为第一个索引values(category, title, id)内的"abcd"做些什么。

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

感谢所有人。

3 个答案:

答案 0 :(得分:48)

以下将为您提供所需的对象

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

对于个人:

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"];

此外,

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

答案 1 :(得分:3)

像这样检查:

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

答案 2 :(得分:0)

这是一个数组,所以请使用objectAtIndex:(即[getcat objectAtIndex:0]getcat[0]