xcode 在 Objective-C 中解析嵌套的 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11368921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse nested JSON Object in Objective-C
提问by anuragbh
Perhaps I am over-thinking or confusing myself, but my head is stuck in a loop over this and I cannot break out.
或许是我想太多了或者让自己困惑了,但是我的脑袋被困在这个循环中,我无法摆脱。
I have a a JSON of the format: (Validated at http://jsonformatter.curiousconcept.com/)
我有一个 JSON 格式:(在http://jsonformatter.curiousconcept.com/验证)
{
id: 1,
sections: "5",
total: "10",
result: {
3: 00PM: [
{
name: "Anurag",
status: "Web"
},
{
name: "Anurag2",
status: "Web2"
}
],
5: 00PM: [
{
name: "Anurag",
status: "Seated"
}
],
6: 00PM: [
{
name: "Anurag4",
status: "Web4"
},
{
name: "Anurag5",
status: "Web5"
},
{
name: "Anurag6",
status: "Web6"
},
{
name: "Anurag7",
status: "Web7"
}
]
}
}
I have this so far:
到目前为止我有这个:
NSDictionary *dict = [response JSONValue];
NSDictionary *results = [dict objectForKey:@"result"];
NSInteger num_results = [[dict valueForKey:@"total"] intValue];
NSInteger num_sections = [[dict valueForKey:@"sections"] intValue];
NSMutableArray *sections = [[NSMutableArray alloc] initWithCapacity:num_sections];
NSMutableArray *objarr = [[NSMutableArray alloc] initWithCapacity:num_results];
NSMutableArray *obj= [[NSMutableArray alloc] initWithCapacity:num_sections];
NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:num_results];
for (NSString* key in results) {
NSLog(@"Key: %@", key); // prints out 3:00 PM...5:00 PM etc...
[obj addObject:[results objectForKey:key]]; // nested objects within each key are saved in the array
NSLog(@"Object 1: %@", obj);
}
for (int i = 0; i < [obj count]; i++) {
//NSLog(@"Object 2: %@", [obj objectAtIndex:i]);
[temp addObject:[obj objectAtIndex:i]]; // I take each object from previous array and save it in a temp array
for (int i = 0; i < num_results; i++) {
NSLog(@"Object 3: %@", [temp objectAtIndex:i]);
**[objarr addObject:[temp objectAtIndex:i]]; // I want to extract the object within the object but cannot get it to work**
}
}
I am able to make an array of objects within each of the 3 keys inside results. But I am not able to get each of the objects inside them as a separate object and save them in an array.
我能够在结果中的 3 个键中的每一个中创建一个对象数组。但是我无法将其中的每个对象作为单独的对象获取并将它们保存在数组中。
For example, in an NSArray I have:
例如,在 NSArray 中,我有:
Object 3: (
{
name = "Anurag ";
status = Web;
},
{
name = "Anurag ";
status = Web;
}
)
How can I get the two objects inside this object and save them both in an array? I guess the main issue is I cannot refer to a key name to get the individual object.
如何获取此对象中的两个对象并将它们保存在数组中?我想主要问题是我无法引用键名来获取单个对象。
回答by Midhun MP
回答by pdesantis
You are redefining int iin your second forloop. Try something like
您正在第二个for循环中重新定义int i。尝试类似
for(int j = 0; j < num_results; j++)
回答by Hot Licks
[temp addObject:[obj objectAtIndex:i]]; // I take each object from previous array and save it in a temp array
This is useless motion. You're effectively just copying the obj
array to the temp
array -- no value added.
这是无用的运动。您实际上只是将obj
数组复制到temp
数组中——没有增加任何价值。