2014-03-06 33 views
1

首先我有一个NSDictionary。我执行NSURLRequest并从请求中加载带有JSON信息的字典。我该如何处理这个Array对象? (Objective-C)

NSDictionary *json =[NSJSONSerialization JSONObjectWithData:response 
                options:kNilOptions 
                 error:&error]; 

本字典有2个键值和2个值。 1键=(_ NSCFString)“games”(value:(_NSCFArray *)),另一个是“id”(玩家)。 因为我想要最近的比赛我做[json objectForKey:@"games"]。现在我得到一个有10个对象的NSArray。第一个对象列在下面。 如何向我的标签说出哪位冠军(来自冠军ID)玩家玩过或他玩过哪种游戏类型?因为这全部在1个对象中。

如何拆分数组以获取所有信息?

我很感激每一个帮助。 谢谢你的时间。

'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吸气)

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。 非常简单,我只是可以找到它。 你的答案非常有帮助!

+2

你的每个数组对象都是'NSDictionary',对吧?你应该能够使用像这样的东西访问数据:'[((NSDictionary *)[games objectAtIndex:0])valueForKey:@“championId”];' – Romain

+0

你有一个标签数组吗?目前还不清楚你在挣扎什么 - 一个细节清单,简单地从字典中获取一个项目... – Wain

+0

阅读转储。 '{}'包围一个字典,'()'包围一个数组。一次剥离一层。如果需要,写一个循环来搜索你的数组。 (是的,在一个语句中有更奇妙的方法来进行搜索,但是如果你编写循环,你会明白你做得更好。) –

回答

0

假设你在你的问题表明的结构,你可以做到以下几点:

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]; 

:在这种情况下,你会做同样的两倍。所以,您可能需要检查导入数据的部分。

+0

顶部是一个名为dict的NSDictionary。在这个字典中有2个键,代表NSArray的关键“游戏”(与10个最近的游戏),另一个键不相关。因此,当我这样做'NSDictionary * game = [games objectAtIndex:0]; 的NSNumber * championID = [游戏objectForKey:@ “championId”];'我会得到一个错误:' - [__ NSCFArray objectForKey:]:无法识别的选择发送到实例0xab6c8e0 2014年3月6日13:55:25.612 ***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [__ NSCFArray objectForKey:]:无法识别的选择发送到实例0xab6c8e0'' – IntegerGaming

+0

@IntegerGaming - 该错误是告诉你,你有一个数组,而不是一个字典。回去再看看转储点,找出你做错了什么。 –

0

这里有一些代码应该可以帮助你解决你的问题。当然,这只是简单的方案,应该可以帮助你解决你的问题。

该solation的主要思想是递归地创建对象。您必须将所需的所有属性添加到游戏和Champion类。

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 
相关问题