问题描述
我正在尝试在iOS中解析以下JSON代码. JSON代码由一系列从0到x的游戏组成.我一直在搜索,但似乎找不到任何具有这种格式的JSON的示例.
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"}
}
我已经创建了一个游戏对象,并且想要为JSON数组中的每个项目创建一个新的游戏对象,并将其添加到游戏数组中.我在解析JSON时遇到了麻烦,感谢您的任何帮助或建议.
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.
我要使用的代码是
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);
当我尝试从JSON数组提取数据时,收到以下错误.-[__ NSCFString objectForKey:]:无法识别的选择器已发送到实例0x1f06f4e0
When I try extracting data from the JSON array, I receive the following error.-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1f06f4e0
预先感谢
尼克
推荐答案
该代码已中断,因为键command
的对象不是字典.
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];
}
我建议在您的Game
类中创建一个initWithDictionary:(NSDictionary *)dictionary
,以获得更简洁的代码.
I would suggest to make an initWithDictionary:(NSDictionary *)dictionary
in your Game
class for more cleaner code.
这篇关于在IOS中解析嵌套的JSON代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!