c# - 使用 Flurl 从 JSON 解析为对象

标签 c# json flurl

我正在使用 Flurl 从 TVDB API 获取 JSON 响应。

我有这门课(现在很笨,只是想获得系列名称):

public class Show
{
    public string seriesName;
}

我是这样使用 Flurl 的:

dynamic test = await "https://api.thetvdb.com/search/series?name=battlestar"
            .WithHeader("Accept", "application/json")
            .WithHeader("Authorization", " Bearer " + token.token)                
            .GetAsync()                
            .ReceiveJson<List<Show>>();

我猜它没有将 JSON 正确映射到对象,因为 JSON 中的“数据”键或者它可能无法将它直接解析为 List<> ?我不确定如何映射它。我想要的结果是 List<Show>Show包含seriesName = "Battlestar Galactica: Blood & Chrome" , seriesName = "Battlestar Galactica (2003)"等等等等

{
  "data": [
    {
      "aliases": [
        "Blood & Chrome",
        "Blood and Chrome"
      ],
      "banner": "graphical/204781-g.jpg",
      "firstAired": "2012-11-09",
      "id": 204781,
      "network": "YouTube",
      "overview": "Battlestar: Blood & Chrome takes place in the 10th year of the first Cylon war. As the battle between humans and their creation, a sentient robotic race, rages across the 12 colonial worlds, a brash rookie viper pilot enters the fray. Ensign William Adama (Pasqualino), barely in his 20's and a recent Academy graduate, finds himself assigned to one of the most powerful ships in the Colonial fleet...the Galactica. The talented but hot-headed risk-taker soon finds himself leading a dangerous top secret mission that, if successful, will turn the tide of the decade-long war in favor of the desperate fleet.",
      "seriesName": "Battlestar Galactica: Blood & Chrome",
      "status": "Ended"
    },
    {
      "aliases": [
        "Battlestar Galactica 2003"
      ],
      "banner": "graphical/73545-g11.jpg",
      "firstAired": "2003-12-08",
      "id": 73545,
      "network": "SciFi",
      "overview": "In a distant part of the universe, a civilization of humans live on planets known as the Twelve Colonies. In the past, the Colonies have been at war with a cybernetic race known as the Cylons. 40 years after the first war the Cylons launch a devastating attack on the Colonies. The only military ship that survived the attack takes up the task of leading a small fugitive fleet of survivors into space in search of a fabled refuge known as Earth.",
      "seriesName": "Battlestar Galactica (2003)",
      "status": "Ended"
    },
    {
      "aliases": [],
      "banner": "graphical/71173-g.jpg",
      "firstAired": "1978-09-17",
      "id": 71173,
      "network": "ABC (US)",
      "overview": "When the 12 Colonies of Man are wiped out by a cybernetic race called the Cylons, Commander Adama (Lorne Greene) and the crew of the battlestar Galactica lead a ragtag fleet of human survivors in search of a \"mythical planet\" called Earth.",
      "seriesName": "Battlestar Galactica",
      "status": "Ended"
    },
    {
      "aliases": [
        "Battlestar Galacticast"
      ],
      "banner": "",
      "firstAired": "2007-03-01",
      "id": 207131,
      "network": "",
      "overview": "Matt + Nat give their weekly BSG episode reviews, discuss theories, interview cast members, and more.",
      "seriesName": "BSGCast",
      "status": "Ended"
    },
    {
      "aliases": [
        "Battlestar Galactica 1980"
      ],
      "banner": "graphical/1252-g.jpg",
      "firstAired": "1980-01-27",
      "id": 71170,
      "network": "ABC (US)",
      "overview": "Set 30 years after Battlestar Galactica, the Galactica is guided by the mysterious teenage genius prodigy Dr. Zee. Adama, sporting a hideously fake beard, remains in command of the fleet, with Col. Boomer his second in command. Upon realizing Earth of 1980 cannot face the Cylons, and hearing Zee's warning that the Cylons followed them, Adama turns the fleet away, sending his grandson Troy (the grown up Boxey) and his wingman Dillon to explore Earth and aid in speeding up its technological development. They are helped by a reporter named Jamie Hamilton, and new technology such as personal cloaking shields and flying motorcycles.",
      "seriesName": "Galactica 1980",
      "status": "Ended"
    }
  ]
}

编辑:这是在 ex.Message 中:

Request to https://api.thetvdb.com/search/series?name=battlestar failed. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[TVDBapi.ShowData]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 9.

最佳答案

您发布的 JSON 不是数据数组,它有一个名为 data 的属性,即数组。这意味着您要反序列化的对象不太正确。你应该试试这个:

public class ShowData
{
    public List<Show> data { get; set; }
}

public class Show
{
    public string[] aliases { get; set; }
    public string banner { get; set; }
    public string firstAired { get; set; }
    public int id { get; set; }
    public string network { get; set; }
    public string overview { get; set; }
    public string seriesName { get; set; }
    public string status { get; set; }
}

然后像这样反序列化:

....ReceiveJson<ShowData>();

关于c# - 使用 Flurl 从 JSON 解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752168/

相关文章:

c# - 从 IEnumerable<ObservableCollection<T>> C# 中提取项目

c# - Quartz.NET - 这个单元测试不应该通过吗?

json - 如何将 PIL 图像转换为 JSON 可序列化字符串,反之亦然?

c# - 使用 Flurl 发布多个标题

C# out 关键字的含义以及它在 PHP 中是否具有等效项?

c# - 通过 ResourceManager GetObject 获取图像——每次都调用它还是存储结果?

javascript - 使用 bodymovin JSON 更改 SVG 上的 View 框值

c# - 将 JSON 对象 A 转换为 JSON B 对象,其中 B 是 A 的严格子集。两者都由两个 json 模式管理。在 .net 核心中

用于多部分 Put 的 Flul 扩展

c# - Flurl PostUrlEncoded 使用 GET 而不是 POST