Question

I'm trying to parse this JSON that looks to me like well written, but NSJSONSerialization doesn't think the same AFAIK since it's returning an NSArray.

This is my code:

NSData* gamesData = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString:@"http://s42sport.com/polarice/json/games.json"]
                        ];

    NSDictionary* json = nil;
    if (gamesData) {
        json = [NSJSONSerialization
                JSONObjectWithData:gamesData
                options:kNilOptions
                error:nil];
        NSLog(@"%d",json.count);
    }

The questions are,

What's wrong with the JSON? Why NSSerialization doesn't return me the NSDictionary?

Edit: Yes, I just learned about the [...] vs {...}. Thank You.

Was it helpful?

Solution

Parse your json by this way.

NSURL * url=[NSURL URLWithString:@"http://s42sport.com/polarice/json/games.json"];

NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;

NSMutableDictionary * json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];

NSLog(@"%@",json);

NSArray * array1=[json valueForKey:@"c"];

NSLog(@"%@",array1);

Try this code. this will surely work for you.

OTHER TIPS

NSDictionnary should be used for Object whereas NSArray is use for JSON array

NSArray* json = nil;
if (gamesData) {
    json = [NSJSONSerialization
            JSONObjectWithData:gamesData
            options:kNilOptions
            error:nil];
    NSLog(@"%d",json.count);
}

The JSON file you listed is an array (it starts and ends with a square bracket) so Objective-C reflects that with an NSArray root object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top