TVMAZE Json的反序列化。顶级数组的类

时间:2019-07-05 06:00:28

标签: c# arrays json json-deserialization

我正在尝试反序列化TVMAZE制作的Json。我已经用“ json2csharp”生成了类,但这并没有实现更高的数组级别。我无法继续下去。

我尝试了多种方法。所附代码中就是其中之一。

CSHARP类

public class Shows
{
    private List<ShowFound> shows;
    public List<ShowFound> foundShows { get => shows; set => shows = value; }
}
public class ShowFound
{

    [JsonProperty("score")]
    public double score { get; set; }

    [JsonProperty("show")]
    public Show show { get; set; }
}

public class Schedule
{

    [JsonProperty("time")]
    public string time { get; set; }

    [JsonProperty("days")]
    public IList<string> days { get; set; }
}

public class Rating
{

    [JsonProperty("average")]
    public double? average { get; set; }
}

public class Country
{

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("code")]
    public string code { get; set; }

    [JsonProperty("timezone")]
    public string timezone { get; set; }
}

public class Network
{

    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("country")]
    public Country country { get; set; }
}

public class Externals
{

    [JsonProperty("tvrage")]
    public int? tvrage { get; set; }

    [JsonProperty("thetvdb")]
    public int thetvdb { get; set; }

    [JsonProperty("imdb")]
    public string imdb { get; set; }
}

public class Image
{

    [JsonProperty("medium")]
    public string medium { get; set; }

    [JsonProperty("original")]
    public string original { get; set; }
}

public class Self
{

    [JsonProperty("href")]
    public string href { get; set; }
}

public class Previousepisode
{

    [JsonProperty("href")]
    public string href { get; set; }
}

public class Nextepisode
{

    [JsonProperty("href")]
    public string href { get; set; }
}

public class Links
{

    [JsonProperty("self")]
    public Self self { get; set; }

    [JsonProperty("previousepisode")]
    public Previousepisode previousepisode { get; set; }

    [JsonProperty("nextepisode")]
    public Nextepisode nextepisode { get; set; }
}

public class Show
{

    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("url")]
    public string url { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }

    [JsonProperty("language")]
    public string language { get; set; }

    [JsonProperty("genres")]
    public IList<string> genres { get; set; }

    [JsonProperty("status")]
    public string status { get; set; }

    [JsonProperty("runtime")]
    public int runtime { get; set; }

    [JsonProperty("premiered")]
    public string premiered { get; set; }

    [JsonProperty("officialSite")]
    public string officialSite { get; set; }

    [JsonProperty("schedule")]
    public Schedule schedule { get; set; }

    [JsonProperty("rating")]
    public Rating rating { get; set; }

    [JsonProperty("weight")]
    public int weight { get; set; }

    [JsonProperty("network")]
    public Network network { get; set; }

    [JsonProperty("webChannel")]
    public object webChannel { get; set; }

    [JsonProperty("externals")]
    public Externals externals { get; set; }

    [JsonProperty("image")]
    public Image image { get; set; }

    [JsonProperty("summary")]
    public string summary { get; set; }

    [JsonProperty("updated")]
    public int updated { get; set; }

    [JsonProperty("_links")]
    public Links _links { get; set; }
}  

代码

public MainWindow()
    {
        InitializeComponent();
        string webURL = "http://api.tvmaze.com/search/shows?q=siren";
        GetJsonAndDeserialize(webURL);
    }

    private void GetJsonAndDeserialize(string webURL)
    {
        WebClient wC = new WebClient();

        // Get a string representation of our JSON
        string jsonText = wC.DownloadString(webURL);

        // Convert the JSON string to a series of Objects
        Shows showCollection = JsonConvert.DeserializeObject<Shows>(jsonText);
        Console.WriteLine(showCollection.foundShows.Count);
    }  

用反弹药炸弹

Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'Tv1.Shows',因为该类型需要JSON对象(例如{“ name”:“ value” })正确反序列化。 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。 路径”,第1行,位置1。'

请帮忙,因为我已经尝试解决了2天。我是初学者。

感谢伦

期望反序列化以创建所描述的类。

0 个答案:

没有答案