[英]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.