获取反序列化Json值的正确名称

本文关键字:反序列化 Json 获取 | 更新日期: 2023-09-27 18:16:17

我试图将我的反序列化Json链接的值绑定到ListView。用户应该搜索一个项目,它将用所有结果填充ListView。

问题是我不确定在ListView代码的{Binding}标签内放什么,我很确定它确实找到了结果(基于许多搜索尝试),但它只是显示空的ListView项目。

这是我到目前为止所尝试的:

<ListView x:Name="ListShows" Margin="49,221,87,-627">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock Text="{Binding Show.name}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Json文件类:

class JsonModel
{
public class Schedule
    {
        public string time { get; set; }
        public List<string> days { get; set; }
    }
    public class Rating
    {
        public double average { get; set; }
    }
    public class Country
    {
        public string name { get; set; }
        public string code { get; set; }
        public string timezone { get; set; }
    }
    public class Network
    {
        public int id { get; set; }
        public string name { get; set; }
        public Country country { get; set; }
    }
    public class Externals
    {
        public int tvrage { get; set; }
        public int thetvdb { get; set; }
        public string imdb { get; set; }
    }
    public class Image
    {
        public string medium { get; set; }
        public string original { get; set; }
    }
    public class Self
    {
        public string href { get; set; }
    }
    public class Previousepisode
    {
        public string href { get; set; }
    }
    public class Nextepisode
    {
        public string href { get; set; }
    }
    public class Links
    {
        public Self self { get; set; }
        public Previousepisode previousepisode { get; set; }
        public Nextepisode nextepisode { get; set; }
    }
    public class Show
    {
        public int id { get; set; }
        public string url { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string language { get; set; }
        public List<string> genres { get; set; }
        public string status { get; set; }
        public int runtime { get; set; }
        public string premiered { get; set; }
        public Schedule schedule { get; set; }
        public Rating rating { get; set; }
        public int weight { get; set; }
        public Network network { get; set; }
        public object webChannel { get; set; }
        public Externals externals { get; set; }
        public Image image { get; set; }
        public string summary { get; set; }
        public int updated { get; set; }
        public Links _links { get; set; }
    }
    public class RootObject
    {
        public double score { get; set; }
        public Show show { get; set; }
    }
}
c#代码:

var httpClient = new HttpClient();
var response = await httpClient.GetAsync("http://api.tvmaze.com/search/shows?q=" + txtSearch.Text);
var result = await response.Content.ReadAsStringAsync();
var results = JsonConvert.DeserializeObject<List<JsonModel.RootObject>>(result, new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        });
ListShows.ItemsSource = results;

任何想法?

获取反序列化Json值的正确名称

您的rootobject:

public class RootObject
{
    public double score { get; set; }
    public Show show { get; set; }
}
Show对象命名为Show 使用

{Binding show.name}
不是

{Binding Show.name}