我如何将大量令人困惑的.json反序列化到一个列表框

本文关键字:反序列化 列表 一个 json | 更新日期: 2023-09-27 17:50:45

我正在与Json.net合作,用电影列表(标题,年份和url到媒体图像)填充ListBox项目,从这个非常长且沉重的json代码:

{(...blabla)"movies":[{"id":4120,"url":"https:'/'/yts.to'/movie'/glee-2009","title":"Glee","year":2009,"medium_cover_image":"https:'/'/s.ynet.io'/assets'/images'/movies'/glee_2009'/medium-cover.jpg","state":"ok",}{(...blabla}

问题是,我刚刚开始使用Json.net,我真的很困惑,我必须做什么。我一直在用我不理解的代码示例转来转去。我能得到一些c#代码来反序列化这些数据在这里:Article article1 = new Article() { Name = "MovieTitle", ImagePath = "URL", Year="0000" };在一个循环?

我如何将大量令人困惑的.json反序列化到一个列表框

处理这个:

  1. 去http://json2csharp.com/并在那里发布你的JSON。

  2. 将生成的类复制到Visual Studio中

  3. JSON中有一个名为"@meta"的命名值,它被翻译成以下无效的属性名称:

    public Meta __invalid_name__@meta { get; set; }
    

    修改如下:

    [JsonProperty("@meta")]
    public Meta Metadata { get; set; }
    
  4. 使用Linq提取所需数据

:

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 long size_bytes { get; set; }
    public string date_uploaded { get; set; }
    public int date_uploaded_unix { 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_long { get; set; }
    public string slug { get; set; }
    public int year { get; set; }
    public double rating { get; set; }
    public int runtime { get; set; }
    public List<string> genres { get; set; }
    public string language { get; set; }
    public string mpa_rating { get; set; }
    public string background_image { get; set; }
    public string small_cover_image { get; set; }
    public string medium_cover_image { get; set; }
    public string state { get; set; }
    public List<Torrent> torrents { get; set; }
    public string date_uploaded { get; set; }
    public int date_uploaded_unix { get; set; }
}
public class Data
{
    public int movie_count { get; set; }
    public int limit { get; set; }
    public int page_number { get; set; }
    public List<Movie> movies { 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; }
}
public class RootObject
{
    public string status { get; set; }
    public string status_message { get; set; }
    public Data data { get; set; }
    [JsonProperty("@meta")]
    public Meta Metadata { get; set; }
}

然后,给定你的类:

public class Article
{
    public string Name { get; set; }
    public string ImagePath { get; set; }
    public string Year { get; set; }
}

使用方式:

        var root = JsonConvert.DeserializeObject<RootObject>(json);
        var articles = root.data.movies.Select(m => new Article { Name = m.title, ImagePath = m.medium_cover_image, Year = m.year.ToString() }).ToList();
        Debug.WriteLine(JsonConvert.SerializeObject(articles, Formatting.Indented));

调试输出为:

[
  {
    "Name": "Glee",
    "ImagePath": "https://s.ynet.io/assets/images/movies/glee_2009/medium-cover.jpg",
    "Year": "2009"
  },
  {
    "Name": "Road Wars",
    "ImagePath": "https://s.ynet.io/assets/images/movies/road_wars_2015/medium-cover.jpg",
    "Year": "2015"
  },
  .... several omitted
]