21

我将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"
        }

     ]
  }
 ]
}

键@“data”的数据可以通过使用保存到数组中

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

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

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

谢谢大家。

4

3 回答 3

51

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

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"];
于 2013-04-26T09:51:11.887 回答
3

像这样检查:

NSString *value=[[[getcate valueForKey:@"abcd"] objectAtIndex:0] valueForKey:@"category"];
于 2013-04-26T09:49:01.533 回答
0

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

于 2013-04-26T09:49:24.493 回答