2

我是服务响应的新手。我无法将其分离"First Class""Second Class" 放入一个单独的数组中。此外,我需要将第一类作为数组,将第二类作为数组。在头等舱中,我需要获取class_idclass_name作为字符串。它将一直持续到。fifteen classes我如何确定响应是first class还是其他second class类?挣扎了很久。提前致谢。

Response :{
"First Class" =     (
            {
        "class_id" = 2;
        "class_name" = "Art";
            },{
        "class_id" = 2;
        "class_name" = "Medical";
            }
        );
   "Second Class" =   (
            {
        "class_id" = 3;
        "class_name" = "Swim";
            }
        )
}
4

2 回答 2

2

试试这个。

根据您的代码,您的服务响应是dictionary. 注意:假设responseDictionary您的服务响应。

如果您的回复有“n”个班级,那么像这样分开每个班级

//get individual classes like this

for(int i=0;i<[[responseDictionary allKeys]count];i++)
{
    NSArray *class = [responseDictionary objectForKey:[[responseDictionary allKeys]objectAtIndex:i]];
}

接着

for(NSDictionary *individualDict in individualClassArray)
{
    NSString *classID = [NSString stringWithFormat:@"%@",[individualDict objectForKey:@"class_id"]];
    NSString *className = [NSString stringWithFormat:@"%@",[individualDict objectForKey:@"class_name"]];
    NSLog(@"class id %@",classID);
    NSLog(@"class name %@", className);
}
于 2013-10-31T12:43:50.937 回答
1

由于您的代码在数组中包含字典而不是字典,因此请尝试这样:-

NSArray *firstClass=[yourDictArray objectAtIndex:0];

for (NSDictionary *dc in firstClass)
{
    NSString *str=[dc objectForKey:@"key"];
}

NSArray *secondClass=[yourDictArray objectAtIndex:1];

for (NSDictionary *dc1 in firstClass)
{
    NSString *str1=[dc1 objectForKey:@"key"];
}
于 2013-10-31T12:09:39.703 回答