Ios 如何处理此数组对象?(目标-C)
首先我有一本字典。我执行一个NSURLRequest,并用请求中的JSON信息加载字典Ios 如何处理此数组对象?(目标-C),ios,objective-c,json,nsarray,nsdictionary,Ios,Objective C,Json,Nsarray,Nsdictionary,首先我有一本字典。我执行一个NSURLRequest,并用请求中的JSON信息加载字典 NSDictionary *json =[NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&am
NSDictionary *json =[NSJSONSerialization JSONObjectWithData:response
options:kNilOptions
error:&error];
此词典有2个键和2个值。1键=\u NSCFString游戏值:\u NSCFArray*另一个是玩家的IDO。
因为我想要我最近玩的游戏[json objectForKey:@games]。现在我得到了一个包含10个对象的NSArray。下面列出了第一个对象。
我如何才能告诉我的标签,球员玩的是championID的哪个冠军,或者他玩的是哪种游戏类型?因为这是一个完整的对象
如何拆分阵列以获取所有信息
我感谢你的帮助。
谢谢你抽出时间
'NSLogged' [ objectAtIndex:0]
{
championId = 102;
createDate = 1394092919791;
fellowPlayers = (
{
championId = 37;
summonerId = 23497430;
teamId = 200;
},
{
championId = 81;
summonerId = 29757887;
teamId = 200;
},
{
championId = 75;
summonerId = 37419112;
teamId = 100;
},
{
championId = 64;
summonerId = 22985242;
teamId = 100;
},
{
championId = 412;
summonerId = 50506867;
teamId = 100;
},
{
championId = 77;
summonerId = 30770067;
teamId = 200;
},
{
championId = 61;
summonerId = 38231891;
teamId = 200;
},
{
championId = 110;
summonerId = 46318423;
teamId = 100;
},
{
championId = 238;
summonerId = 51117008;
teamId = 100;
}
);
gameId = 1359691076;
gameMode = CLASSIC;
gameType = "MATCHED_GAME";
invalid = 0;
level = 30;
mapId = 1;
spell1 = 4;
spell2 = 14;
stats = {
assists = 3;
championsKilled = 4;
goldEarned = 12280;
goldSpent = 11945;
item0 = 1055;
item1 = 3153;
item2 = 3075;
item3 = 3143;
item4 = 3047;
item5 = 1011;
item6 = 3340;
killingSprees = 2;
largestKillingSpree = 2;
largestMultiKill = 1;
level = 17;
magicDamageDealtPlayer = 68999;
magicDamageDealtToChampions = 5146;
magicDamageTaken = 2044;
minionsKilled = 226;
neutralMinionsKilled = 13;
neutralMinionsKilledEnemyJungle = 12;
neutralMinionsKilledYourJungle = 1;
numDeaths = 1;
physicalDamageDealtPlayer = 71900;
physicalDamageDealtToChampions = 2948;
physicalDamageTaken = 19358;
team = 200;
timePlayed = 1814;
totalDamageDealt = 141513;
totalDamageDealtToChampions = 8708;
totalDamageTaken = 21469;
totalHeal = 3243;
totalTimeCrowdControlDealt = 276;
totalUnitsHealed = 1;
trueDamageDealtPlayer = 614;
trueDamageDealtToChampions = 614;
trueDamageTaken = 66;
turretsKilled = 4;
wardPlaced = 8;
win = 1;
};
subType = "CAP_5x5";
teamId = 200; }
更新1:
self.getRecentGames返回一个NSDictionary,它不是self.recentGames的getter
self.recentGames = [[NSDictionary alloc] initWithDictionary:self.getRecentGames];
NSDictionary *dict = [self.recentGames objectForKey:@"games"];
//now I my dict have 10 object but dict is from type: __NSCFArray
更新2:完成!
非常感谢您抽出时间。在我检查了数组中的对象所属的类之后,让它显示它是一个NSDictionary。
这么简单,我就能找到答案。
你的回答很有帮助 假设您在问题中显示的结构,您可以执行以下操作:
NSDictionary *game = [games objectAtIndex:0]; // That you did already
NSNumber *championID = [game objectForKey:@"championId"];
根据您的评论进行编辑:
就像前面提到的用户热舔一样,如果您遇到这样的异常,您可能会假设您有一个字典,但最终会得到一个数组。在这种情况下,您将执行两次相同的操作:
NSArray *game = [games objectAtIndex:0]; // That you did already
NSNumber *championID = [game objectAtIndex:indexOfChampionId];
尽管我怀疑后者是否是你真正想要的。因此,您可能需要检查导入数据的部分。以下是一些代码,这些代码可以帮助您解决问题。当然,这只是一个简单的方案,可以帮助你解决问题 这种隔离的主要思想是递归地创建对象。您必须将所有需要的属性添加到游戏和冠军类中
NSArray *json =[NSJSONSerialization JSONObjectWithData:response
options:kNilOptions
error:&error];
NSMutableArray *games = [NSMutableArray array];
for (NSDictionary *gameDictionary in json) {
Game *game = [[Game alloc] initWithJSONDictionary:gameDictionary];
[games addObject:game];
}
//games is the table which your game objects
///Game class
@interface Game
@property (strong, nonatomic) NSArray *champions;
- (id)initWithJSONDictionary:(NSDictionary *)dictionary;
@end
@implementation Game
- (id)initWithJSONDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
NSMutableArray *array = [NSMutableArray array];
NSArray *champions = dictionary[@"championId"];
foreach (NSDictionary *championDictionary in champions) {
Champion *champion = [[Champion alloc] initWithJSONDictionary:championDictionary];
[array addObject:champion];
}
self.champions = array;
}
return self;
}
@end
/// Champion class
@interface Champion
@property (strong, nonatomic) NSString *championId;
@property (strong, nonatomic) NSString *summonerId;
@property (strong, nonatomic) NSString *teamId;
- (id)initWithJSONDictionary:(NSDictionary *)dictionary;
@end
@implementation Champion
- (id)initWithJSONDictionary:(NSDictionary *)dictionary
{
self = [super init];
if (self) {
self.championId = dictionary[@"championId"];
self.teamId = dictionary[@"teamId"];
self.summonerId = dictionary[@"summonerId"]l
}
return self;
}
@end
每个数组对象都是NSDictionary,对吗?您应该能够使用以下内容访问内部数据:[NSDictionary*[games objectAtIndex:0]valueForKey:@championId];你有一系列的标签吗?现在还不清楚你到底在挣扎什么——一张详细的清单,或者仅仅从字典中获取一个条目……阅读转储文件。{}包围字典,包围数组。一次剥一层。如果需要,编写一个循环来搜索数组。是的,在一个语句中有一些更奇特的搜索方法,但是如果你编写循环,你会更好地理解你在做什么。顶部是一个名为dict的NSDictionary。在这个dict中有两个键,代表最近10个游戏的NSArray,另一个键不相关。因此,当我做这个NSDictionary*游戏=[games objectAtIndex:0];NSNumber*championID=[game objectForKey:@championID];我将得到一个错误:-[\uu NSCFArray objectForKey:]:未识别的选择器发送到实例0xab6c8e0 2014-03-06 13:55:25.612***由于未捕获的异常“NSInvalidArgumentException”,终止应用程序,原因:'-[\uu NSCFArray objectForKey:]:发送到实例0xab6c8e0'@IntegerGaming的选择器无法识别-该错误告诉您有一个数组,而不是字典。请返回并再次查看转储文件,找出错误所在。