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

时间:2016-11-04 17:12:15

标签: c# json listview uwp

我正在尝试将我的反序列化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;

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的根对象:

public class RootObject
{
    public double score { get; set; }
    public Show show { get; set; }
}

由于显示对象名为显示

使用

{Binding show.name}

而不是

{Binding Show.name}