[英]ios - get values from NSDictionary
我的服務器上有JSON,它被解析為iOS應用程序到NSDictionary。 NSDictionary看起來像這樣:
(
{
text = Aaa;
title = 1;
},
{
text = Bbb;
title = 2;
}
)
我的問題是 - 如何從第一維獲得文本,所以它應該是“Aaa”。 我試過用這個:
[[[json allValues]objectAtIndex:0]objectAtIndex:0];
但它沒有用,它以錯誤結束
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]: unrecognized selector sent to instance 0x714a050'
那么請你幫我一下,如何從指定的索引中獲取一個值? 謝謝!
該錯誤消息只是告訴您NSDictionary
(該數組的第一個對象,以及第二個)不響應objectAtIndex
。
這將有點像,但它解釋得更好:
NSArray *jsonArray = [json allValues];
NSDictionary *firstObjectDict = [jsonArray objectAtIndex:0];
NSString *myValue = [firstObjectDict valueForKey:@"text"];
您的JSON對象是一個包含兩個詞典的數組。 這就是獲取價值的方法:
NSDictionary* dict1= json[0];
NSString* text= dict1[@"text"];
NSString* title= dict1[@"title"];
嘗試這個:
NSString *txt = [[json objectAtIndex:0] objectForKey:@"text"];
更新:已修復錯誤。 謝謝yunas。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.