【问题标题】:Parsing two parts of YouTube JSON解析 YouTube JSON 的两个部分
【发布时间】:2015-01-10 01:41:42
【问题描述】:

我尝试解析以下 youtube JSON,以便可以访问标题、描述等内容... https://gdata.youtube.com/feeds/api/videos?q=remedyLIVE&max-results=5&v=2&alt=jsonc&orderby=published

现在我把它放在一个字典中,它返回两个值/键对。一个用于 api,另一个用于其他所有内容。

我将在下面附上我的代码,但我知道它很接近但不太正确。谁能提供一个关于如何解析这个json的具体例子?我找不到任何关于如何做到这一点的好例子,所以如果有人可以帮助我访问这些值,那就太棒了。我花了无数个小时阅读 JSON 教程,但我仍然无法弄清楚它如何适用于我正在做的事情。谢谢!!

    NSURL *youtubeURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos?q=remedyLIVE&max-results=5&v=2&alt=jsonc&orderby=published"];


    NSData *data = [NSData dataWithContentsOfURL:youtubeURL];
        if (data == nil)
        {
            NSLog(@"data is nil");
        }
        else
        {
            NSError *error;
            titleArray = [[NSMutableArray alloc]init];
            videoIDArray = [[NSMutableArray alloc]init];
            thumbArray = [[NSMutableArray alloc]init];

            videoDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
            NSLog(@"%@", videoDictionary);




        }


}

【问题讨论】:

    标签: ios objective-c json cocoa-touch youtube


    【解决方案1】:

    试试这个:

    +(void)json{
    
    NSURL *youtubeURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos?q=remedyLIVE&max-results=5&v=2&alt=jsonc&orderby=published"];
    
    
    NSData *data = [NSData dataWithContentsOfURL:youtubeURL];
    NSError *error;
    NSDictionary *dic_JSON =
    [NSJSONSerialization JSONObjectWithData: data
                                    options: NSJSONReadingMutableContainers
                                      error: &error];
    
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      这是一个例子。

      NSDictionary *subDict = [videoDictionary objectForKey:@"data"];
      if (subDict) {
          NSArray *subArray = [subDict objectForKey:@"items"];
          if ([subArray count]) {
              for (NSDictionary *dictItem in subArray) {
                  NSString *title = [dictItem objectForKey:@"title"];
                  NSString *description = [dictItem objectForKey:@"description"];
                  NSLog(@"title is %@, description is %@", title, description);
              }
          }
      }
      

      【讨论】:

      • 谢谢,这正是我需要的!
      猜你喜欢
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 2013-02-12
      • 2015-01-09
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多