解码Feedly JSON数据

时间:2014-12-30 13:03:43

标签: json xcode nsdictionary

我目前有以下代码来解码一些收到的JSON数据。但是,由于Feedly没有提供任何索引,我不知道如何正确解码JSON数据。这是我收到的JSON示例:

[
  {
    "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/tech",
    "label": "tech"
  },
  {
    "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/design",
    "label": "design"
  }
]

如果我让下面的代码运行,NSLog完美地输出它。但是,如何从label获取res变量?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of notifications",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];

    NSLog(@"res value contains: %@", res);
}

1 个答案:

答案 0 :(得分:0)

如果获得label值的空格为[res valueForKeyPath:@"label"];

但要使其以正确的方式工作,您需要将res的类型更改为NSArray,因为提供的JSON包含字典数组。

然后你可以用for-in遍历。

NSError *myError = nil;
    NSArray *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
for (NSDictionary *d in res) {
    NSLog(@"label %@",[d objectForKey:@"label"]);
}
    NSLog(@"res value contains: %@", res);