使用Flurl将JSON解析为对象

7

我正在使用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中的"data"键或者它不能直接解析为一个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.
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>();

这些更改解决了异常问题,但 ShowData.Shows 是一个空列表。 - Jrow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接