使用NSdictionary中的数据初始化NSArray

我从URL接收数据,把它放在NSDictionary,然后我需要为每个标签创buildNSArray。 我的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

代码中的问题,我试图初始化一个数组,它看起来像我的字典组成的数组。 如何正确提取数据?

为此,你必须遍历你拥有的Array对象。 您可以使用快速枚举

  for(object in JsonArray){ NSString *value = [object valueForKey:@"logo"]valueForKey:@"text"]]; //Add this value to your array } } 

希望这可以帮助。

你得到的JSONResponse是NSArray不是NSDictionary
你可以像这样parsing你的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 )