JSON文件解析提供对象名称而不是内容

时间:2014-11-19 00:28:24

标签: ios objective-c json parsing

我有一个我在这里创建的JSON文件:http://www.jsoneditoronline.org/?id=d0b62425c78f98db2398ed558f92e5cf

简单地说,我将此文件添加到我的项目并尝试解析它。由于某种原因,NSLog系列给了我"声明"结果而不是声明对象。我期待它给我整个语句对象的字典。我在这里显然遗漏了一些东西。我将不胜感激任何帮助。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"math" ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];

 NSLog(@"jsonObject is %@", jsonObject); //this gives me the whole JSON object correctly.

for (NSDictionary *dict in jsonObject[@"statements"]) {
        NSLog(@"dict is %@", dict);

}

1 个答案:

答案 0 :(得分:1)

这是一个棘手的JSON。通常人们会期望“语句”的值是一个对象数组,而不仅仅是一个对象。当你迭代字典(JSON“对象”)时,你会遍历键,所以你得到了预期的结果。