【问题标题】:Obtaining YouTube Statistics Using JSON (UWP, C#)使用 JSON (UWP, C#) 获取 YouTube 统计数据
【发布时间】:2023-04-02 14:19:01
【问题描述】:

自从我在这里发帖以来已经有一段时间了。我正在开发一个应用程序,需要使用 JSON(C#、UWP)获取特定视频的统计信息。

我已经将统计信息作为 JSON 字符串,但似乎无法使用 Newtonsoft 正确解析它们。该值始终返回 null。这是我的代码:

var url = "https://www.googleapis.com/youtube/v3/videos?id=" + videoId + "&key=" + cl.googleAPIKey + "&part=snippet,contentDetails,statistics,status";
var http = new HttpClient();
var response = await http.GetStringAsync(url);

var statistics = JsonConvert.DeserializeObject<Statistics>(response);

string totalViews = statistics.viewCount;

Json2CSharp 生成的类有:

public class PageInfo
{
    public int totalResults { get; set; }
    public int resultsPerPage { get; set; }
}

public class Default
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Medium
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class High
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Standard
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Maxres
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Thumbnails
{
    public Default @default { get; set; }
    public Medium medium { get; set; }
    public High high { get; set; }
    public Standard standard { get; set; }
    public Maxres maxres { get; set; }
}

public class Localized
{
    public string title { get; set; }
    public string description { get; set; }
}

public class Snippet
{
    public DateTime publishedAt { get; set; }
    public string channelId { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public Thumbnails thumbnails { get; set; }
    public string channelTitle { get; set; }
    public List<string> tags { get; set; }
    public string categoryId { get; set; }
    public string liveBroadcastContent { get; set; }
    public Localized localized { get; set; }
}

public class ContentDetails
{
    public string duration { get; set; }
    public string dimension { get; set; }
    public string definition { get; set; }
    public string caption { get; set; }
    public bool licensedContent { get; set; }
    public string projection { get; set; }
}

public class Status
{
    public string uploadStatus { get; set; }
    public string privacyStatus { get; set; }
    public string license { get; set; }
    public bool embeddable { get; set; }
    public bool publicStatsViewable { get; set; }
}

public class Statistics
{
    public string viewCount { get; set; }
    public string likeCount { get; set; }
    public string dislikeCount { get; set; }
    public string favoriteCount { get; set; }
    public string commentCount { get; set; }
}

public class Item
{
    public string kind { get; set; }
    public string etag { get; set; }
    public string id { get; set; }
    public Snippet snippet { get; set; }
    public ContentDetails contentDetails { get; set; }
    public Status status { get; set; }
    public Statistics statistics { get; set; }
}

public class RootObject
{
    public string kind { get; set; }
    public string etag { get; set; }
    public PageInfo pageInfo { get; set; }
    public List<Item> items { get; set; }
}

正如我所说,无论视频 ID 如何,该值都将始终返回为 null。

有人有什么想法吗?

谢谢

【问题讨论】:

  • 你能把response字符串的内容也发一下吗?
  • 确定获得的 JSON 与您要转换的类兼容吗?

标签: c# json youtube uwp json.net


【解决方案1】:

您正在反序列化到错误的类。 Json2Csharp 生成了许多类,但 Statistics 在树的下方。您应该反序列化 RootObject

var root = JsonConvert.DeserializeObject<RootObject>( response );

然后导航到生成的类结构中的统计信息,例如:

foreach ( var statistics in root.Items.Select( i => i.Statistics) )
{
    //do something
} 

【讨论】:

  • 这很好用,感谢您的帮助。我会将其标记为正确答案:)
  • 太棒了:-)。编码愉快!
  • 虽然我的代码不是开源的,但我已经在 cmets 中感谢您的帮助(只是为了表示感谢):) pasteboard.co/H7l1Hhj.png
  • 你真好:-)!谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 2021-08-23
  • 2015-10-03
  • 2016-08-05
  • 2012-02-28
  • 2013-01-16
  • 2019-04-15
  • 2023-04-03
相关资源
最近更新 更多