如何纠正这个错误 class is not key value coding in ios?

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

我需要在以下 JSON 响应中迭代 "c"id,"project" id,name:

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

我试过了

id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainerserror:nil];
NSLog(@"%@",jsonObjects);
if ([jsonObjects objectForKey:@"a"] != [NSNull null]) {
  NSArray *itemArray = [jsonObjects objectForKey:@"a"];
  for (NSDictionary *itemDic in itemArray){
    NSString * user;
    NSArray *user_data =[itemDic valueForKey:@"c"];
    user = [user_data valueForKey:@"id"];
    NSLog(@"%@",user);
  }
}

哪个会抛出错误

This class is not key value coding complaint for the key c.

然后我试了

NSDictionary *dictionary =[NSJSONSerializationJSONObjectWithData:jsonSource  options:kNilOptions error:&error];
NSArray * time_entrid = [dictionary valueForKeyPath:@"a.c.id"];
NSLog(@"%@",time_entrid);

如何解决我上面提到的错误并正确迭代?使用第二种方式是否也可以让我进行迭代?

NSArray *array_c = [json objectForKey: @"c"];

for(NSDictionary *dictn_c in array_c)

{

NSString *id_str = [dictn_c objectForKey:@"id"];

NSDictionary *project_dictn = [dictn_c objectForKey: @"project"];

NSString *project_id_str = [project_dictn objectForKey:@"id"];

} 这应该有效

NSArray *array_c = [jsonObjects objectForKey:@"c"];

NSArray *id_arr = [array_c valueForKey:@"id"];

这将为您提供 "id"

的数组

你有一个 NSArray 和 NSDictionary 对于 "a" NSDictionary 使用(你不能迭代 NSDictionary 唯一的方法是)

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

你会得到1,通过名字改变id你会得到b

对于 NSArray

[[[[dictionary objectForKey:@"a"] objectForKey:@"c"] objectAtIndex:0] objectForKey:@"id"]

您将获得 id 768

项目编号

[[[[[dictionary objectForKey:@"a"] objectForKey:@"c"] objectAtIndex:0] objectForKey:@"project"] objectForKey:@"id"]

你会得到8个

[[[[[dictionary objectForKey:@"a"] objectForKey:@"c"] objectAtIndex:0] objectForKey:@"project"] objectForKey:@"name"]

你会得到名字

迭代数组使用

for(int i=0; i<[[[dictionary objectForKey:@"a"] objectForKey:@"c"] count];i++)
{

NSLog(@"%@", [[[[dictionary objectForKey:@"a"] objectForKey:@"c"] objectAtIndex:i] objectForKey:@"id"]);
}