2016-11-04 29 views
0

我想将反序列化的Json链接的值绑定到ListView。用户应该搜索一个项目,并将所有结果填入ListView。获取反序列化的Json值的正确名称

问题是,我不确定要在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; 

任何想法?

+0

您的反序列化工作是否正常? – Dudemanword

+0

我刚刚添加了一个断点,它似乎工作正常@Dudemanword –

回答

1

你rootobject:

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

由于显示对象被命名为显示

使用

{Binding show.name} 

,而不是

{Binding Show.name}