0

I'm having issues seperating how to handle two JSOn responses. IF there is only one element returned, it gives a different response than two:

Here is one result:

{
    "voteMap": {
        "SID": "2727",
        "CNT": "1",
        "VTD": "N"
    }
}

And two:

{
    "voteMap": [
        {
            "SID": "2814",
            "CNT": "1",
            "VTD": "N"
        },
        {
            "SID": "2815",
            "CNT": "1",
            "VTD": "N"
        }
    ]
}

I go from having one array to having an array of dictionaries. Here is how I am handling it:

responseArr = jsonArray[@"voteMap"];

  if([jsonArray count] == 1){
       Song* song = [self findSong:[responseArr valueForKey:@"SID"]];
       song.votes =[responseArr valueForKey:@"CNT"];
       song.status = [responseArr valueForKey:@"VTD"];
        }           
   else{
         for (NSDictionary *dict in responseArr) {
            Song *song = [self findSong:[dict valueForKey:@"SID"]];
            song.votes =[dict valueForKey:@"CNT"];
            song.status = [dict valueForKey:@"VTD"];
            }
        }

Sorry about the bad formatting. What is happening is the [jsonArray count] == 1 is always happening...presumable because it always returns voteMap

4

5 回答 5

1

我建议使用以下内容检查类型:

id object = [response objectForKey:@"voteMap"];
if ([object isKindOfClass:[NSArray class]]) {
    // process multible objects
} else if ([object isKindOfClass:[NSDictionary class]]) {
    // process individual object
}
于 2013-08-27T04:36:46.413 回答
1
responseArr = jsonArray[@"voteMap"];
    if ([responseArr isKindOfClass:[NSArray class]]) {
        for (NSDictionary *dict in responseArr) {
            Song *song = [self findSong:[dict valueForKey:@"SID"]];
            song.votes =[dict valueForKey:@"CNT"];
            song.status = [dict valueForKey:@"VTD"];
            }
    }
    if ([responseArr isKindOfClass:[NSDictionary class]]) {
Song* song = [self findSong:[responseArr valueForKey:@"SID"]];
       song.votes =[responseArr valueForKey:@"CNT"];
       song.status = [responseArr valueForKey:@"VTD"];
        }  

    }

我认为这会对你有所帮助。

于 2013-08-27T04:37:18.887 回答
1

您可以使用isKindOfClass方法来确定它是 aNSArrayNSDictionary

于 2013-08-27T04:37:41.373 回答
1

尝试以下代码。并检查 json 是数组还是字典

id jsonArray;
if([jsonArray isKindOfClass:[NSArray class]]){

    Song* song = [self findSong:[responseArr valueForKey:@"SID"]];
    song.votes =[responseArr valueForKey:@"CNT"];
    song.status = [responseArr valueForKey:@"VTD"];
}
else{
    for (NSDictionary *dict in responseArr) {
        Song *song = [self findSong:[dict valueForKey:@"SID"]];
        song.votes =[dict valueForKey:@"CNT"];
        song.status = [dict valueForKey:@"VTD"];
    }
}
于 2013-08-27T04:38:27.370 回答
1
NSArray* responseArr = [response objectForKey:@"voteMap"];
if ([responseArr isKindOfClass:[NSDictionary class]]) {
   responseArr = [NSArray arrayWithObject:responseArr];
}
for (NSDictionary *dict in responseArr) {
    Song *song = [self findSong:[dict valueForKey:@"SID"]];
    song.votes =[dict valueForKey:@"CNT"];
    song.status = [dict valueForKey:@"VTD"];
}

(我已经多次使用过这个模型。)

于 2013-08-27T04:42:44.127 回答