objective-c:读取json创建的数组

时间:2013-01-18 08:50:26

标签: objective-c json nsarray nsdictionary

我从Ios dev开始,我遇到了一些麻烦来操纵Json创建的数组: 我在我的应用程序中调用了一个Web服务,它返回了我的数据:

{evenements =(
             ({
            dateEvenement ={
                1 = "01-01-2013";
                2 = "02-01-2013";
                3 = "03-01-2013";
                4 = "04-01-2013";
            };
            idEvenement = 61;
            nbrInvite = 1;
            nomEvenement = "My event Name";
            nomUtilisateur = "Lucas ";
        }
    ),
 );
}

除了“dateEvenement”之外,我可以通过以下代码获取所有值:

NSArray *msgList;
msgList = [ jsonResults objectForKey:@"evenements" ];

for (NSDictionary *evenements in msgList) {

    for (NSDictionary *evenement in evenements ) {

        NSString *idEvenement = [evenement objectForKey:@"idEvenement"];
        NSString *nomUtilisateur = [evenement objectForKey:@"nomUtilisateur"];
        NSString *nomEvenement = [evenement objectForKey:@"nomEvenement"];
        NSString *nbrInvite = [evenement objectForKey:@"nbrInvite"];

        NSArray *dates = [ evenement objectForKey:@"dateEvenement" ];
     }
}

你能帮我获取“dateEvenement”的数据

吗?

1 个答案:

答案 0 :(得分:1)

在您的JSON中,dateEvenement不是数组而是字典:

NSDictionary *dates = [ evenement objectForKey:@"dateEvenement"];

for(NSNumber *key in dates) {
   NSString *dateString = [dates objectForKey:key];
   NSLog(%@ : %@, key, dateString); 
}

正如您在JSON示例中声明的那样,字典的键是数字,因此您应NSNumber对象作为键类型。