[英]Initialize NSArray with data from NSdictionary
我从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由数组组成。 如何正确提取数据?
为此,您必须遍历您拥有的Array对象。 您可以使用快速枚举
for(object in JsonArray){
NSString *value = [object valueForKey:@"logo"]valueForKey:@"text"]];
//Add this value to your array
}
}
希望这可以帮助。
你得到的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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.