【问题标题】:JSON Parsing C# (Array?) [duplicate]JSON解析C#(数组?)[重复]
【发布时间】:2016-03-28 09:03:36
【问题描述】:

我这里真的什么都没有。浏览了与 C# 和 JSON 有关的各种问题。我尝试过干预,我只能 A: 获取所有 JSON 输出,或者什么都没有,我似乎无法弄清楚如何得到我想要的。

所以我从这里拉 JSON:https://yts.ag/api/v2/list_movies.json?limit=1&quality=720p 为了方便起见,我只将它限制为 1,您可以增加它以查看代码是否可以扩展。

现在,我要做的是,键入来自 URL 的键/值对和该键的值。

【问题讨论】:

标签: c# json json.net


【解决方案1】:

您可能可以使用此类获取完整数据并对其进行反序列化

public class Rootobject
{
    public string status { get; set; }
    public string status_message { get; set; }
    public Data data { get; set; }
    public Meta meta { get; set; }
}

public class Data
{
    public int movie_count { get; set; }
    public int limit { get; set; }
    public int page_number { get; set; }
    public Movie[] movies { get; set; }
}

public class Movie
{
    public int id { get; set; }
    public string url { get; set; }
    public string imdb_code { get; set; }
    public string title { get; set; }
    public string title_english { get; set; }
    public string title_long { get; set; }
    public string slug { get; set; }
    public int year { get; set; }
    public float rating { get; set; }
    public int runtime { get; set; }
    public string[] genres { get; set; }
    public string summary { get; set; }
    public string description_full { get; set; }
    public string synopsis { get; set; }
    public string yt_trailer_code { get; set; }
    public string language { get; set; }
    public string mpa_rating { get; set; }
    public string background_image { get; set; }
    public string background_image_original { get; set; }
    public string small_cover_image { get; set; }
    public string medium_cover_image { get; set; }
    public string large_cover_image { get; set; }
    public string state { get; set; }
    public Torrent[] torrents { get; set; }
    public string date_uploaded { get; set; }
    public int date_uploaded_unix { get; set; }
}

public class Torrent
{
    public string url { get; set; }
    public string hash { get; set; }
    public string quality { get; set; }
    public int seeds { get; set; }
    public int peers { get; set; }
    public string size { get; set; }
    public int size_bytes { get; set; }
    public string date_uploaded { get; set; }
    public int date_uploaded_unix { get; set; }
}

public class Meta
{
    public int server_time { get; set; }
    public string server_timezone { get; set; }
    public int api_version { get; set; }
    public string execution_time { get; set; }
}

像这样反序列化 JSON

Rootobject JsonObj = JsonConvert.DeserializeObject<Rootobject>(json);

然后你可以提取任何你想要的数据。

string movieURL = JsonObj.data.movies[0].url;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 2013-03-25
    • 1970-01-01
    • 2023-04-11
    • 2020-09-30
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多