【发布时间】:2014-11-19 00:28:24
【问题描述】:
我确实有一个我在这里创建的 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);
}
【问题讨论】:
-
这个的输出是什么:` NSLog(@"dict is %@", dict);` ?
-
当您遍历字典时,返回的值就是键。
-
@MidhunMP 输出是“语句”
-
(那是奇怪的 JSON。通常人们会期望“语句”是一个对象数组,而不仅仅是一个对象。)
-
(你得到了预期的结果,考虑到扭曲的 JSON。)
标签: ios objective-c json parsing