Parse nested JSON code in IOS

烂漫一生 提交于 2019-12-13 06:20:35

问题


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


回答1:


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.




回答2:


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"



来源:https://stackoverflow.com/questions/16024163/parse-nested-json-code-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!