LenSala
LenSala

Reputation: 3

How do I access the data resulting from JSON Deserialization

I have deserialized JSON data provided by tvmaze. However I don't know how to access the data. I am a beginner.

I have tried: deserializedCollection. deserializedCollection[0]. TVShows. TVShows[0. none of these compile. I am lost.

Here are the classes:

public class TVShows
{
    [JsonProperty("score")]
    public double score { get; set; }

    [JsonProperty("show")]
    public Show show { get; set; }
}

public class Country
{
    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("code")]
    public string code { get; set; }

    [JsonProperty("timezone")]
    public string timezone { get; set; }
}

public class Externals
{
    [JsonProperty("tvrage")]
    public int? tvrage { get; set; }

    [JsonProperty("thetvdb")]
    public int thetvdb { get; set; }

    [JsonProperty("imdb")]
    public string imdb { get; set; }
}

public class Image
{
    [JsonProperty("medium")]
    public string medium { get; set; }

    [JsonProperty("original")]
    public string original { get; set; }
}

public class Links
{
    [JsonProperty("self")]
    public Self self { get; set; }

    [JsonProperty("previousepisode")]
    public Previousepisode previousepisode { get; set; }

    [JsonProperty("nextepisode")]
    public Nextepisode nextepisode { get; set; }
}

public class Network
{
    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("country")]
    public Country country { get; set; }
}

public class Nextepisode
{
    [JsonProperty("href")]
    public string href { get; set; }
}

public class Previousepisode
{
    [JsonProperty("href")]
    public string href { get; set; }
}

public class Rating
{
    [JsonProperty("average")]
    public double? average { get; set; }
}

public class Schedule
{
    [JsonProperty("time")]
    public string time { get; set; }

    [JsonProperty("days")]
    public IList<string> days { get; set; }
}

public class Self
{
    [JsonProperty("href")]
    public string href { get; set; }
}

public class Show
{
    [JsonProperty("id")]
    public int id { get; set; }

    [JsonProperty("url")]
    public string url { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }

    [JsonProperty("language")]
    public string language { get; set; }

    [JsonProperty("genres")]
    public IList<string> genres { get; set; }

    [JsonProperty("status")]
    public string status { get; set; }

    [JsonProperty("runtime")]
    public int runtime { get; set; }

    [JsonProperty("premiered")]
    public string premiered { get; set; }

    [JsonProperty("officialSite")]
    public string officialSite { get; set; }

    [JsonProperty("schedule")]
    public Schedule schedule { get; set; }

    [JsonProperty("rating")]
    public Rating rating { get; set; }

    [JsonProperty("weight")]
    public int weight { get; set; }

    [JsonProperty("network")]
    public Network network { get; set; }

    [JsonProperty("webChannel")]
    public object webChannel { get; set; }

    [JsonProperty("externals")]
    public Externals externals { get; set; }

    [JsonProperty("image")]
    public Image image { get; set; }

    [JsonProperty("summary")]
    public string summary { get; set; }

    [JsonProperty("updated")]
    public int updated { get; set; }

    [JsonProperty("_links")]
    public Links _links { get; set; }
}  

Here is the main part of the code:

IList deserializedCollection = new ArrayList();
deserializedCollection = JsonConvert.DeserializeObject<List<TVShows>>(json);  

Can some please show me how to access:

1) "score" in the first occurrence of TVShows 2) "name" in the first occurrence of Show within the first occurrence of TVShows.

These should help me to proceed.

Contents of the properties.

Upvotes: 0

Views: 61

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

You need to hold the result in List<Shows> :

List<TVShows> deserializedCollection = JsonConvert.DeserializeObject<List<TVShows>>(json); 

and then you can use either FirstOrDefault() or indexing like deserializedCollection[0].score

for example:

TVShows show = deserializedCollection.FirstOrDefault();
double score = show.score;

or:

TVShows show = deserializedCollection[0]

The first one is better as it saves from runtime exceptions in case there is no item in the collection.

For accessing each show you will need to loop which can either be for or foreach

foreach(var show in deserializedCollection)
{
   var score = show.score;
}

Hope it gives some idea.

Upvotes: 1

Related Questions