【问题标题】:Getting correct names of deserialized Json values获取反序列化 Json 值的正确名称
【发布时间】:2017-03-18 14:52:43
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 你的反序列化工作正常吗?
  • 我刚刚添加了一个断点,它似乎工作正常@Dudemanword

标签: c# json listview uwp


【解决方案1】:

你的根对象:

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

由于 Show 对象被命名为 show

使用

{Binding show.name}

而不是

{Binding Show.name}

【讨论】:

    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多