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