简体   繁体   中英

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

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.

您需要将类型gameItem“ NSMutableDictionary ”更改为“ id ”并添加检查if ([gameItem isKindOfClass: [NSDictionary Class]]) ,在这种情况下,请初始化“ obj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM