2 yue1liang1chuan yue1liang1chuan 于 2013.03.05 10:39 提问

AFNetworking valueForKeyPath通配符

我正在从下面的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(其中*就是通配符)

1个回答

Mr_me
Mr_me   2013.03.05 11:40
已采纳

你需要去遍历值/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]]

Csdn user default icon
上传中...
上传图片
插入图片