Objective c 迭代并映射JSON字典以构建对象数组-目标C
我从JSOn web服务得到以下响应:Objective c 迭代并映射JSON字典以构建对象数组-目标C,objective-c,ios5,Objective C,Ios5,我从JSOn web服务得到以下响应: categoria = ( { "id_categoria" = 61; imagen = "http://exular.es/mcommerce/image/data/ipod_classic_4.jpg"; nombre = Gorras; }, { "id_catego
categoria = (
{
"id_categoria" = 61;
imagen = "http://exular.es/mcommerce/image/data/ipod_classic_4.jpg";
nombre = Gorras;
},
{
"id_categoria" = 59;
imagen = "http://exular.es/mcommerce/image/data/ipod_touch_5.jpg";
nombre = Camisetas;
},
{
"id_categoria" = 60;
imagen = "http://exular.es/mcommerce/image/data/ipod_nano_1.jpg";
nombre = Pantalones;
}
);
}
在这段代码中,它是字典中的提要:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//data =nil;
NSLog(@"JSNON %@", responseString);
NSDictionary *results = [responseString JSONValue];
NSLog(@"JSNON %@", results);
我需要迭代字典中的所有类别,并创建一个NSArray,其中包含属性为“name”和“image”的对象类别
非常感谢
NSArray *categories = [results objectForKey:@"categoria"];
这将创建一个类别对象数组。您可以将此字典/对象数组传递给自定义对象,也可以根据需要对其进行迭代
Category *cat = [[Category alloc] initWithDictionary:[categories objectAtIndex:0]];
这只是假设您的init设置为处理字典。没有足够的代码可以知道,但您应该能够从这里找到答案。谢谢Bill,这非常有帮助。