Matt J Matt J - 28 days ago 11
C# Question

Getting correct names of deserialized Json values

I am trying to bind the values from my deserialized Json link to a ListView. The user is supposed to search for an item and which will fill the ListView with all the results.

The problem is that I'm not sure what to put within the {Binding } tag on the ListView code, I am pretty sure that it does find the results (based on many search attempts) but it just shows empty ListView items.

This is what I've tried so far:

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


Class of Json file:

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# code:

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;


Any ideas?

Jim Jim
Answer

Your rootobject:

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

Since the Show object is named show

Use

{Binding show.name}

instead of

{Binding Show.name}
Comments