我从URL接收数据,将其放在NSDictionary中,然后我需要为每个标签创建NSArray。我的NSDictionary看起来像:
(
{
id = {
text = 1;
};
logo = {
text = "url 1";
};
name = {
text = "some name 1";
};
},
{
id = {
text = 2;
};
logo = {
text = "url 2";
};
name = {
text = "some name 2";
};
},
{
id = {
text = 3;
};
logo = {
text = "url 3";
};
name = {
text = "some name 3";
};
}
)
我希望有这样的数组:arrLogo =(@“url 1”,@“url 2”,“url 3”)。
我正在做下一个:
arrName=[[dict objectForKey:@"name"] valueForKey:@"text"];
但是Xcode给了我一个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance
我正在尝试初始化数组的代码中的问题,看起来我的dict由数组组成。如何正确提取数据?
答案 0 :(得分:2)
为此,您必须遍历您拥有的Array对象。您可以使用快速枚举
for(object in JsonArray){
NSString *value = [object valueForKey:@"logo"]valueForKey:@"text"]];
//Add this value to your array
}
}
希望这有帮助。
答案 1 :(得分:1)
您获得的JSONResponse是NSArray
而非NSDictionary
你可以像这样解析你的json响应......
NSMutableArray * tmpAry = your json response;
NSMutableArray * urlAry = [[NSMutableArray alloc] init];
for (int i=0; i<tmpAry.count; i++) {
NSMutableDictionary * tmpDictn = [tmpAry objectAtIndex:i];
[urlAry addObject:[[tmpDictn objectForKey:@"logo"] valueForKey:@"text"]];
}
NSLog(@"urlAry : %@",urlAry);
日志将显示:
urlAry : (
url 1,
url 2,
url 3
)