如何将大量令人困惑的 .JSON 反序列化为列表框?
How can I deserialize tons of confusing .JSON to a listbox?
我正在与 Json.net 合作,用电影列表(标题、年份和 url 到 medium-image)填充 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" };
in a loop?
处理这个:
转到 http://json2csharp.com/ 和 post 你的 JSON 那里。
将生成的 classes 复制到 Visual Studio。
JSON 中有一个名为 "@meta"
的命名值,它被转换为以下无效的 属性 名称:
public Meta __invalid_name__@meta { get; set; }
修复如下:
[JsonProperty("@meta")]
public Meta Metadata { get; set; }
使用 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; }
}
然后,给定你的 class:
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
]
我正在与 Json.net 合作,用电影列表(标题、年份和 url 到 medium-image)填充 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" };
in a loop?
处理这个:
转到 http://json2csharp.com/ 和 post 你的 JSON 那里。
将生成的 classes 复制到 Visual Studio。
JSON 中有一个名为
"@meta"
的命名值,它被转换为以下无效的 属性 名称:public Meta __invalid_name__@meta { get; set; }
修复如下:
[JsonProperty("@meta")] public Meta Metadata { get; set; }
使用 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; }
}
然后,给定你的 class:
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 ]