无法绕过解析嵌套的JSON [英] Can't get head around parsing nested JSON

查看:139
本文介绍了无法绕过解析嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a hard time trying to get my head around this here:

所以我有以下JSON:

so I have the following 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的方式:

And this is how I am parsing this 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"值.

What I can't seem to understand is how can I access the first value inside "thumbnail_images", which is "full" and then get its 'url' value.

推荐答案

JSONObjectWithData方法创建一个由嵌套的NSArray s和NSDIctionary s组成的对象.要提取感兴趣的项目,您必须向下钻取对象,重复提取数组和字典,直到获得所需的内容.将现代语法用于数组和字典使此操作相对容易.

The JSONObjectWithData method creates an object that consists of nested NSArrays and NSDIctionarys. To extract items of interest you have to drill down into the object, repeatedly extracting arrays and dictionaries until you reach whatever it is that you want. Using the modern syntax for arrays and dictionaries makes this relatively easy.

还请注意,仅当JSONObjectWithData的返回值为nil时,错误返回才有效.

Also note that the error return is only valid if the return value from JSONObjectWithData is 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];

这篇关于无法绕过解析嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆