1

Here's my post.json file:

[
    {
        "Title": "Introduction to WCF",
        "Url": "http://myaddress/videos/introduction-to-wcf",
        "Thumbnail": "http://myaddress/images/20110212_01.jpg",
        "Exceprt": "Introduction to WCF",
        "PostDate": "2011-02-12T14:26:07",
        "Id": 39,
        "Mp4Video": "http://myaddress/2012/05/20110212_01.mp4",
        "Speakers": [
            {
                "Name": "Mark Wilkinson",
                "Slug": "mark-wilkinson"
            }
        ],
        "Groups": [
            {
                "Name": "C# UG",
                "Slug": "cs-ug"
            }
        ],
        "Tags": [
            {
                "Name": "WCF Services",
                "Slug": "wcf-services"
            }
        ]
    }
]

post this in jsonlint.org and it validates.

Here's the code that I've been using for other JSON files that has worked:

- (void)test_can_read_from_groups_file_and_build_JSONDictionary {

    id result = [self data_from_JSON_file:@"post"];
    [Assert isNotNil:result];  // is returning as nil, so test is failing
}

- (id)data_from_JSON_file:(NSString *)fileName {

    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSString *jsonString = [bundle pathForResource:fileName ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:jsonString];
    JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];

    NSError *error = nil;
    id result =  [decoder objectWithData:data error:&error];
    if (error) {
        NSLog(@"*********\r\r\r\r\r\r\r Error was: %@", [error localizedDescription]);
    }

    return result;
}

error that is printed out from JSONKit objectWithData:

Error was: Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.

ETA: yes it's in the build phases:

enter image description here

added:

if (!data)
{
    NSLog(@"\r\r\r\r\r\r%s: data was nil", __FUNCTION__);
    return nil;
}

It's not hitting this branch so data is not nil.

Changed using JSONKit decoder to this:

id results = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions error:&error];

and it works, still perplexed as to why JSONKit is failing for me but not for Rob.

Mark W
  • 3,769
  • 2
  • 34
  • 52
  • 1
    Was "" - perhaps there are some hidden/control/special characters (e.g. BOM) not copied over when validating on JSONLint? –  Mar 27 '13 at 01:34
  • Convert "data" to a string and log it, to be sure it's all there. – Hot Licks Mar 27 '13 at 16:08

1 Answers1

1

As pst pointed out, the problem turned out to be the BOM. In Xcode, if you right-click on the filename, choose "Open As" and choose "Hex", you'll see:

hex dump

Those first three characters are obviously not standard text characters. Fortunately, you can highlight these three characters in the hex editor in Xcode, delete them, save the file, and it will now that should fix it.


Original answer:

Also, are you sure the JSON was included in your bundle (check "Copy Bundle Resources" in the "Build Phases" of your "Target Settings). I just parsed your JSON with Cocoa standard JSON parsing class, NSJSONSerialization, without incident. Perhaps you should try examining the data and make sure everything is ok:

NSLog(@"data=%@", [[[NSString alloc] initWithData:data] autorelease]);

But I parsed your JSON with both JSONKit and NSJSONSerialization without incident.

NSString *filename = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filename];
if (!data)
{
    NSLog(@"%s: data was nil", __FUNCTION__);
    return;
}
JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
NSError *error = nil;
id results =  [decoder objectWithData:data error:&error];

//  Also tested with NSJSONSerialization
//
//    id results = [NSJSONSerialization JSONObjectWithData:data
//                                                 options:0
//                                                   error:&error];

if (!error)
    NSLog(@"%s: results = %@", __FUNCTION__, results);
else
    NSLog(@"%s: error = %@", __FUNCTION__, error);
Community
  • 1
  • 1
Rob
  • 371,891
  • 67
  • 713
  • 902
  • JSONKit makes it very clear that you must adhere *strictly* to the rules of JSON, while `NSJSONSerialization` is more loosy-goosy about it. – borrrden Mar 27 '13 at 01:56
  • 1
    @borrrden I just tried the supplied JSON with `JSONKit` and it worked fine. Personally, I'd like to see Mark verify that the `data` was loaded successfully before further diagnostics are done. – Rob Mar 27 '13 at 01:59
  • added the pic above to show the .json files that I'm testing with are added to the target build phases 'Copy Bundle Resources' – Mark W Mar 27 '13 at 14:20
  • also added the test on if(!data) and it did not go into this condition so data is not nil. – Mark W Mar 27 '13 at 14:30
  • @MarkW Did you try `NSLog` of the string from the `NSData`? Does it look ok? Assuming it does, can you upload your JSON somewhere we can tell that there's nothing curious in the file (as pst suggested)? – Rob Mar 27 '13 at 16:51
  • @MarkW Also, if you've done any editing/tweaking of your JSON files outside of Xcode, you will want to run "Clean" from Xcode's "Product" menu, and then rebuild the product, because Xcode can sometimes get confused about what needs to be added to the bundle otherwise. – Rob Mar 27 '13 at 17:19
  • yeah the NSData *data prints out it's columns of numbers. So it's not nil. I clean pretty often to cancel out that possiblity. Just cleaned again, tried using the decoder from JSONDecoder from JSONKit and it's nil, still. – Mark W Mar 27 '13 at 19:38
  • @MarkW let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27034/discussion-between-rob-and-mark-w) – Rob Mar 27 '13 at 19:40
  • 1
    Rob solved it, basically there was extra characters at the start of the file that could not be seen, but were added by Xcode when creating the file from an empty file template and pasting the json in. Lesson be, create text files in another editor if possible. – Mark W Mar 27 '13 at 20:11
  • To give credit where credit is due, pst pegged it in his comment to your question. – Rob Mar 27 '13 at 20:21