JSON文件读取3840错误

时间:2016-09-03 17:22:56

标签: objective-c json

读取此文件city.list.us.json.gz(未压缩)会收到错误(3840):

    NSString * path = [mb pathForResource:@"city.list.us" ofType:@"json" inDirectory:@"JSON"];
    NSString * string = [NSString stringWithContentsOfUTF8File:path];
    NSData * data = [string dataUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"isValidJSONObject:%@", [NSJSONSerialization isValidJSONObject:data] ? @"Yes" : @"No");
    NSError * bobo = nil;
    id blob = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&bobo];
    [NSApp presentError:bobo];

读取ok - 长度1873878字节,在vi(set list)中看起来没问题,但不会产生非零对象。

文件的头部-4显示:

{"_id":4070245,"name":"Jones Crossroads","country":"US","coord":{"lon":-85.484657,"lat":31.21073}}
{"_id":4344544,"name":"Vernon Parish","country":"US","coord":{"lon":-93.183502,"lat":31.11685}}
{"_id":4215307,"name":"Pennick","country":"US","coord":{"lon":-81.55899,"lat":31.313}}
{"_id":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}}

对于我的非专家JSON眼睛,它似乎是一个引用文件,每行有一个对象,有4个值(_id,name,country,coord),最后一个包含2个值的对象(lat,lon)。

还尝试了NSASCIIStringEncoding进行NSData转换,但没有任何乐趣。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

那是无效的JSON。它是大量的JSON词典,但它们彼此无关。

很可能预期的JSON(字典数组)应该看起来像

[{"_id":4070245,"name":"Jones Crossroads","country":"US","coord":{"lon":-85.484657,"lat":31.21073}},
{"_id":4344544,"name":"Vernon Parish","country":"US","coord":{"lon":-93.183502,"lat":31.11685}},
{"_id":4215307,"name":"Pennick","country":"US","coord":{"lon":-81.55899,"lat":31.313}},
{"_id":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}}]

我将整个文本包装在方括号[]中,并在每行的末尾添加逗号。