NSDictionaries和嵌套的JSON [英] NSDictionaries and Nested JSON
问题描述
我能够从JSON文件创建NSDictionary没问题,我需要知道的是如何做嵌套的JSON字符串?参见示例:
I am able to create a NSDictionary from a JSON file no problem, what I need to know is how to do nested JSON strings?? See Example:
{
"data": [
{
"id": "270639882984792_306265986160413",
"from": {
"category": "Non-profit organization",
"name": "My Facebook Page",
"id": "270639882984792"
},
因此,这是大型JSON文件的一部分,但是如何设置它,以便可以从"from"键调用"name"键-我知道如何从数据"键,但是我想要的是更深一层.
So this is part of a massive JSON file, but how do I set it up, so that I can call the "name" key from the "from" key - I know how to call the "id" key from the "data" key, but the one I want is a level deeper.
预先感谢:-)
编辑-这是我开始处理的一些iOS代码:
EDIT - here is some iOS code I started working on:
NSDictionary *items = [json objectForKey:@"data"];
NSArray *items2 = [items objectForKey:@"from"];
// NSDictionary *item = [items objectAtIndex:1];
NSMutableArray *story = [NSMutableArray array];
for (NSDictionary *item in items2 )
{
if([item objectForKey:@"name"] || [item objectForKey:@"name"] != nil || [[item objectForKey:@"name"] length]>0){
[story addObject:[item objectForKey:@"name"]];
}
NSLog(@"ITEM - %@", [item objectForKey:@"name"]);
}
推荐答案
尝试一下:
NSDictionary* data= json[@"data"];
NSDictionary* from= [data[0] objectForKey: @"from"];
NSString* name= from[@"name"];
如果您只想使用名称就可以了,而如果您需要所有数据,那么我建议创建一个对象,该对象能够为每个字典条目保留一个值.您可以使用与我显示的相同的方式访问"id"和"category",只是使用不同的键.
If you just want the name you're fine, if instead you need all the data, I suggest to create an object that is able to hold a value for every dictionary entry. You access to "id" and "category" the same way I shown, just using a different key.
这篇关于NSDictionaries和嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!