6

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

我有这门课(现在被愚弄了,只是想得到 seriesNames):

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>sShow包含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.
4

1 回答 1

12

您发布的 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>();
于 2016-08-03T19:37:30.123 回答