无法解析嵌套的JSON

时间:2014-06-06 01:41:08

标签: ios objective-c json nsdictionary

我很难在这里试图解决这个问题:

所以我有以下JSON:

"posts": [
{
  "id": 42400,
  "type": "post",
  "url": "http://dummy.com/noticias/2014/06/senado-promulga-emenda-contra-trabalho-escravo-42400/",
  "status": "publish",
  "title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
  "title_plain": "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
  "content": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>",
  "excerpt": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>",
  "date": "2014-06-05 16:59:55",
  "modified": "2014-06-05 17:00:42",
  "author": {
    "id": 2,
    "slug": "author",
    "name": "Author",
    "first_name": "Author",
    "last_name": "",
    "nickname": "Author",
    "url": "",
    "description": ""
  },
  "thumbnail_images": {
    "full": {
      "url": "http://dummy.com/wp-content/uploads/2014/06/pec-trabalho-escravo.jpg",
      "width": 585,
      "height": 390
    },
    "thumbnail": {
      "url": "http://dummy.com/wp-content/uploads/2014/06/pec-trabalho-escravo-110x110.jpg",
      "width": 110,
      "height": 110
    },
    "medium": {
      "url": "http://dummy.com/wp-content/uploads/2014/06/pec-trabalho-escravo-230x130.jpg",
      "width": 230,
      "height": 130
    },
    "large": {
      "url": "http://dummy.com/wp-content/uploads/2014/06/pec-trabalho-escravo-585x360.jpg",
      "width": 585,
      "height": 360
    },
    "slider-thumb": {
      "url": "http://dummy.com/wp-content/uploads/2014/06/pec-trabalho-escravo-520x390.jpg",
      "width": 520,
      "height": 390
    }
  }  
},

这就是我解析这个JSON的方式:

NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

if (localError != nil ) {
    *error = localError;
    return nil;
}

NSMutableArray *posts = [[NSMutableArray alloc] init];
NSArray *results = [parsedObject valueForKey:@"posts"];

for (NSDictionary *postDic in results) {

    Data *data = [[Data alloc] init];
    for (NSString *key in postDic) {
        if ([data respondsToSelector:NSSelectorFromString(key)]) {
            [data setValue:[postDic valueForKey:key] forKey:key];
        }
    }

    [posts addObject:data];
}

return posts;

我似乎无法理解的是如何访问“thumbnail_images”中的第一个值,即“full”,然后获取其“url”值。

3 个答案:

答案 0 :(得分:0)

关键&#34;帖子&#34;有一个NSArray它的值,这是你的变量命名结果。结果数组的每个索引都可以包含数组或字典。

thumbnail_images位于索引1处,因此您可以像NSDictionary *thumbImages = results[1];

那样获取它

从现在开始,您可以像这样访问所需的所有嵌套键。所以对于#34;完整&#34;然后&#34; url&#34;:NSString *url = thumbImages[@"thumbnail_images"][@"full"][@"url"];

答案 1 :(得分:0)

JSONObjectWithData方法创建一个由嵌套的NSArrayNSDIctionary组成的对象。要提取感兴趣的项目,您必须深入查看对象,重复提取数组和字典,直到达到您想要的任何内容。使用数组和字典的现代语法使这相对容易。

另请注意,只有JSONObjectWithData的返回值为nil时,错误返回才有效。

NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

if ( parsedObject == nil )
{
    *error = localError;
    return nil;
}

NSMutableArray *results = [NSMutableArray new];
NSArray *posts = parsedObject[@"posts"];

for ( NSDictionary *post in posts )
{
    NSDictionary *thumb = post[@"thumbnail_images"];
    NSDictionary *full  = thumb[@"full"];
    NSString *urlString = full[@"url"];
    NSLog( @"%@", urlString );

    [results addObject:urlString];
}

return [results copy];

答案 2 :(得分:-1)

您可以检查对象是否是字典,然后遍历它:

for (NSDictionary *postDic in results) {

Data *data = [[Data alloc] init];
for (NSString *key in postDic) {
    if ([data respondsToSelector:NSSelectorFromString(key)]) {
        if ([results[postDic] isMemberOfClass:[NSDictionary class]) {
            // At this point you will get the "full", "thumbnail", etc dictionaries
            NSDictionary *childDict = results[postDic];
            //Now you can just do the same thing once again to get the url value.
        }
        [data setValue:[postDic valueForKey:key] forKey:key];
    }
}

[posts addObject:data];
}