Nick Hardman
Nick Hardman

Reputation: 15

Parse nested JSON code in IOS

I'm trying to parse the following JSON code in iOS. The JSON code consists of an array of games numbered 0 to x. I've been searching around but cant seem to find any examples which have JSON in this format.

{
"command":"show_my_games",
"0":{"gameid":"7","foruserid":"16","againstuserid":"0","casefor":"","caseagainst":"","status":"0","argumentid":"7","againstusername":null,"forusername":"Gem","argument":"argument 1"},
"1":{"gameid":"8","foruserid":"15","againstuserid":"16","casefor":"","caseagainst":"","status":"0","argumentid":"23","againstusername":"Gem","forusername":"Nick","argument":"argument 2"},
"2":{"gameid":"9","foruserid":"18","againstuserid":"16","casefor":"","caseagainst":"","status":"0","argumentid":"26","againstusername":"Gem","forusername":"Nick","argument":"argument 3"}
}

I've created a game object, and for each item in the JSON array, I want to create a new game object, and add this to the game array. I'm having trouble parsing the JSON, and i'd appreciate any help or advice.

The code i'm trying to use is

            NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];

            for (NSMutableDictionary * gameItem in json) {
                Game *obj = [Game new];
                obj.GameID=[gameItem objectForKey:@"gameid"];
                obj.Argument=[gameItem objectForKey:@"argument"];
                obj.CaseFor=[gameItem objectForKey:@"casefor"];
                obj.CaseAgainst=[gameItem objectForKey:@"caseagainst"];
                obj.CaseForOwner=[gameItem objectForKey:@"forusername"];
                obj.CaseAgainstOwner=[gameItem objectForKey:@"againstusername"];
                obj.CaseForOwnerID=[gameItem objectForKey:@"foruserid"];
                obj.CaseAgainstOwnerID=[gameItem objectForKey:@"againstuserid"];
                //add to the players game array
                [myGameArray addObject:obj];

            }
            NSLog(@"array: %@", myGameArray);

When I try extracting data from the JSON array, I receive the following error. -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1f06f4e0

Thanks in advance,

Nick

Upvotes: 0

Views: 1624

Answers (2)

developer
developer

Reputation: 11

You need to change the type gameItem "NSMutableDictionary" to "id" and add the checking - if ([gameItem isKindOfClass: [NSDictionary Class]]) and in the case of this condition initialize "obj"

Upvotes: 1

Anupdas
Anupdas

Reputation: 10201

The code is breaking as the object for the key command is not a dictionary.

You can modify the code like this.

    for (NSString *key in json){
        id object = json[key];
        if ([object isKindOfClass:[NSDictionary class]]) {
           Game *obj = [Game new];
           obj.GameID=[gameItem objectForKey:@"gameid"];
           obj.Argument=[gameItem objectForKey:@"argument"];
           obj.CaseFor=[gameItem objectForKey:@"casefor"];
           obj.CaseAgainst=[gameItem objectForKey:@"caseagainst"];
           obj.CaseForOwner=[gameItem objectForKey:@"forusername"];
           obj.CaseAgainstOwner=[gameItem objectForKey:@"againstusername"];
           obj.CaseForOwnerID=[gameItem objectForKey:@"foruserid"];
           obj.CaseAgainstOwnerID=[gameItem objectForKey:@"againstuserid"];
           //add to the players game array
          [myGameArray addObject:obj];
    }

I would suggest to make an initWithDictionary:(NSDictionary *)dictionary in your Game class for more cleaner code.

Upvotes: 1

Related Questions