Parsing from json to object using FLURL

僤鯓⒐⒋嵵緔 提交于 2020-01-03 17:31:09

问题


I'm using FLURL to get a JSON response from the TVDB api.

I have this class(dumbed down for now, just trying to get the seriesNames):

public class Show
{
    public string seriesName;
}

And I'm using FLURL like this:

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

I'm guessing it doesn't map the JSON to the object correctly because of the "data" key in the JSON or maybe it can't parse it straight into a List<>? I'm not sure how to map this. The result I want would be a List<Show> with the Shows containing seriesName = "Battlestar Galactica: Blood & Chrome", seriesName = "Battlestar Galactica (2003)", etc, etc.

{
  "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"
    }
  ]
}

EDIT: This is in 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:


The JSON you have posted isn't an array of data, it has a property called data that is the array. This means your object to deserialise to is not quite right. You should try this:

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; }
}

And then deserialise like this:

....ReceiveJson<ShowData>();


来源:https://stackoverflow.com/questions/38752168/parsing-from-json-to-object-using-flurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!