[英]How to parse NSDictionary of unnamed arrays
這是字典dict
的NSLog
結果:
(
{
namelists = {
watchlist = (a, b, c, ... )
}
},
{
namelists = {
watchlist = (x, y, z, ... )
}
}
)
我如何獲得監視列表數組? 當我嘗試這個:
NSAarray *array = dict[@"nameLists"][@"watchlist"]
我收到錯誤消息:“無法識別的選擇器已發送到實例”。 我認為未命名數組不在這里引用。 我如何獲得監視列表數組? 提前致謝。
您覺得自己正在處理字典。 但這是一系列字典。 所以你要:
NSArray *jsonObject = ... // get that main object however you want, presumably NSJSONSerialization
NSArray *watchlist = jsonObject[0][@"namelists"][@"watchlist"]
要么
for (NSDictionary *dict in jsonObject)
{
NSArray *watchlist = dict[@"namelists"][@"watchlist"];
// now do something with watchlist
}
這個數據結構可以像這樣編碼
NSArray *arrayOfNamelists = @[@{@"namelists" : @{@"watchlist" : @[@"a",@"b",@"c"]}},
@{@"namelists" : @{@"watchlist" : @[@"x",@"y",@"z"]}}];
並且可以像這樣訪問字符串數組中的元素
NSString *entry = [[arrayOfNamelists[1] objectForKey:@"namelists"]
objectForKey:@"watchlist"][2];
在此示例中給出“ z”
要檢查這個
NSLog(@"%@\ncount = %i",arrayOfNamelists,arrayOfNamelists.count);
for(NSDictionary *namelistDictionary in arrayOfNamelists){
NSDictionary *watchlistDictionary = [namelistDictionary objectForKey:@"namelists"];
NSArray *watchlistsArray = [watchlistDictionary objectForKey:@"watchlist"];
for(NSString *watchlistEntry in watchlistsArray){
NSLog(@"%@",watchlistEntry);
}
};
給
(
{
namelists = {
watchlist = (
a,
b,
c
);
};
},
{
namelists = {
watchlist = (
x,
y,
z
);
};
}
)
計數= 2
一種
b
C
X
ÿ
ž
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.