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;
任何想法?
您的反序列化工作是否正常? – Dudemanword
我剛剛添加了一個斷點,它似乎工作正常@Dudemanword –