slashlos slashlos - 11 months ago 53
Objective-C Question

JSON file read 3840 error

Reading this file (uncompressed) gets an error (3840) doing:

NSString * path = [mb pathForResource:@"" 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];

which reads ok - length 1873878 bytes, looks ok in vi (set list), but does not yield a non-nil object.

A head -4 of the file shows:

{"_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":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}}

To my un-expert JSON eyes, it appears this is a file of cites, has an object per line with 4 values (_id,name,country,coord), last an object containing 2 values (lat,lon).

Also tried NSASCIIStringEncoding for the NSData conversion but no joy.

Any ideas?

Answer Source

That's not valid JSON. It's a large number of JSON dictionaries but they are not related to each other.

Most likely the expected JSON (an array of dictionaries) is supposed to look like

[{"_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":5285039,"name":"Black Bear Spring","country":"US","coord":{"lon":-110.288139,"lat":31.386209}}]

I wrapped the whole text in square brackets [] and added commas at the end of each line.