Как получить доступ к данным, полученным в результате десериализации JSON - PullRequest
0 голосов
/ 06 июля 2019

Я десериализовал данные JSON, предоставленные tvmaze.Однако я не знаю, как получить доступ к данным.Я новичок.

Я пробовал: deserializedCollection.deserializedCollection [0].ТВ шоу.TVShows [0.ни один из них не компилируется.Я потерян.

Вот классы:

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; }
}  

Вот основная часть кода:

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

Могут ли некоторые показать мне, как получить доступ:

1) «оценка» в первом появлении ТВ-шоу 2) «имя» в первом появлении «Шоу» в первом появлении ТВ-шоу.

Это должно помочь мне продолжить.

Содержание недвижимости.

1 Ответ

1 голос
/ 06 июля 2019

Вам нужно сохранить результат в List<Shows>:

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

, а затем вы можете использовать либо FirstOrDefault(), либо индексировать как deserializedCollection[0].score

, например:

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

или:

TVShows show = deserializedCollection[0]

Первый вариант лучше, так как он экономит на исключениях времени выполнения, если в коллекции нет элементов.

Для доступа к каждому шоу вам понадобится цикл, который может быть либо for, либо foreach

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

Надеюсь, это даст некоторую идею.

...