我正在從下面的JSON文件中獲取tvrage_name
:
{
data: {
Band of Brothers: {
air_by_date: 0,
cache: {
banner: 1,
poster: 1
},
language: "en",
network: "HBO",
next_ep_airdate: "",
paused: 0,
quality: "Any",
status: "Ended",
tvdbid: 74205,
tvrage_id: 2708,
tvrage_name: "Band of Brothers"
},
Breaking Bad: {
air_by_date: 0,
cache: {
banner: 1,
poster: 1
},
language: "en",
network: "AMC",
next_ep_airdate: "2013-07-14",
paused: 0,
quality: "HD",
status: "Continuing",
tvdbid: 81189,
tvrage_id: 18164,
tvrage_name: "Breaking Bad"
},
},
message: "",
result: "success"
}
能不能在valueForKeyPath
中有一個通配符?這樣valueForKeyPath
可以成為:data.*.tvrage_name
(其中*就是通配符)
你需要去遍歷值/key,像這樣:
...
NSDictionary *data = [JSON valueForKey:@"data"];
for (NSString *movie in data) {
NSLog(@"tvrage_name: %@", [[data valueForKey:movie] valueForKey:@"tvrage_name"]);
}
...
為了保險起見我會做額外的檢查e.g. [JSON isKindOfClass:[NSDictionary class]]