从NSDictionary检索密钥

时间:2015-11-02 13:13:46

标签: ios objective-c json key nsdictionary

我在查询TMDb时很难设法从JSON响应中取出任何内容。尽管周末大部分时间都在搜索这些论坛和其他论坛,但我并不是一个解决方案。

在我的头文件中,我有:

@property (nonatomic,strong) NSDictionary * fetchedData;

字典正确填充,因为当我使用时,JSON响应的第一部分看起来像这样:

NSLog(@"fetchedData: %@", fetchedData);


fetchedData: {
  page = 1;
  results = (
    {
      adult = 0;
      id = 1245;
      "known_for" = (
        {
          adult = 0;
          "backdroppath" = "/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg";
          "genreids" = ( 878, 28, 12 );
          id = 24428;
          "mediatype" = movie;
          "original_language" = en;
          "original_title" = "The Avengers";

我一直在尝试多种方法来检索" original_title"的所有实例。响应中的(或任何键),但是当每次尝试返回NULL时我都没有留下任何头发,所以欢迎任何建议!

2 个答案:

答案 0 :(得分:3)

试试这个。

NSString *title = fetchedData[@"results"][0][@"known_for"][0][@"original_title"];

答案 1 :(得分:1)

从响应中获取数据

我认为fetchedData是dictionary.So

NSString *strOriginalTitle = [NSString stringWithFormat:@"%@",[[[[[fetchedData valueForKey:@"results"]objectAtIndex:0]valueForKey:@"known_for"]objectAtIndex:0]valueForKey:@"original_title"];

      //OR By getting data step by step

NSArray *arrayResults = [fetchedData valueForKey:@"results"];
NSDictionary *dict = [[arrayResults objectAtIndex:0]  copy];
NSArray *arrayKnownFor = [dict valueForKey@"known_for"] copy];
NSString *strOriginalTitle = [arrayKnownFor objectAtIndex:0]valueForKey:@"original_title"];
NSLog(@"The original_title is - %@",strOriginalTitle);