反序列化嵌套 JSON,C#
Deserialize nested JSON, C#
我需要用这种格式反序列化一些 JSON:
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.29062294960022,
"available": 10000
},
"body": {
"track_list": [
{
"track": {
"track_id": 45085706,
"track_name": "Love Overdose (Deboa & Hannah Holland Remix)",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_name": "Dance"
}
}
]
}
}
}
]
}
}
}
我有这些 类 是我从在线生成器那里得到的,所以我认为它们没问题。
public class Header
{
public int status_code { get; set; }
public double execute_time { get; set; }
public int available { get; set; }
}
public class MusicGenre
{
public int music_genre_id { get; set; }
public int music_genre_parent_id { get; set; }
public string music_genre_name { get; set; }
public string music_genre_name_extended { get; set; }
public string music_genre_vanity { get; set; }
}
public class MusicGenreList
{
public MusicGenre music_genre { get; set; }
}
public class PrimaryGenres
{
public List<MusicGenreList> music_genre_list { get; set; }
}
public class Track
{
public int track_id { get; set; }
public string track_name { get; set; }
public List<object> track_name_translation_list { get; set; }
public int track_rating { get; set; }
public int commontrack_id { get; set; }
public int instrumental { get; set; }
public int @explicit { get; set; }
public int has_lyrics { get; set; }
public int has_subtitles { get; set; }
public int has_richsync { get; set; }
public int num_favourite { get; set; }
public int album_id { get; set; }
public string album_name { get; set; }
public int artist_id { get; set; }
public string artist_name { get; set; }
public string track_share_url { get; set; }
public string track_edit_url { get; set; }
public int restricted { get; set; }
public DateTime updated_time { get; set; }
public PrimaryGenres primary_genres { get; set; }
}
public class TrackList
{
public Track track { get; set; }
}
public class Body
{
public List<TrackList> TrackList { get; set; }
}
public class Message
{
public Header header { get; set; }
public Body body { get; set; }
}
public class Root
{
public Message message { get; set; }
}
我尝试使用以下代码反序列化 JSON:
using (StreamReader r = new StreamReader(@"c:\users\xxxx\desktop.json"))
{
string json = r.ReadToEnd();
var tracks = JsonConvert.DeserializeObject<Track>(json);
}
但我一无所获。我是新来的;用更简单的 JSON 实现了它,但我无法弄清楚如何使用这段代码来实现它。我想打印一个只有歌曲名称的列表。
如果有人能帮助我,我将不胜感激!
这里有几个问题:
在您的 Body
class 中,TrackList
属性 与 JSON 不匹配。 JSON中对应的属性称为track_list
。 class 属性必须与 JSON 完全匹配(忽略大小写),否则您需要在 属性 上使用 [JsonProperty]
属性来指示 JSON名字会。例如:
public class Body
{
[JsonProperty("track_list")]
public List<TrackList> TrackList { get; set; }
}
您正在尝试反序列化为 Track
class,但您应该反序列化为 Root
,因为它代表 [=41= 的根].
var root = JsonConvert.DeserializeObject<Root>(json);
反序列化为 Root
后,您可以“向下钻取”以打印出轨道。
foreach (var item in root.message.body.TrackList)
{
Console.WriteLine(item.track.track_name);
}
Fiddle: https://dotnetfiddle.net/JnljGU
我需要用这种格式反序列化一些 JSON:
{
"message": {
"header": {
"status_code": 200,
"execute_time": 0.29062294960022,
"available": 10000
},
"body": {
"track_list": [
{
"track": {
"track_id": 45085706,
"track_name": "Love Overdose (Deboa & Hannah Holland Remix)",
"primary_genres": {
"music_genre_list": [
{
"music_genre": {
"music_genre_name": "Dance"
}
}
]
}
}
}
]
}
}
}
我有这些 类 是我从在线生成器那里得到的,所以我认为它们没问题。
public class Header
{
public int status_code { get; set; }
public double execute_time { get; set; }
public int available { get; set; }
}
public class MusicGenre
{
public int music_genre_id { get; set; }
public int music_genre_parent_id { get; set; }
public string music_genre_name { get; set; }
public string music_genre_name_extended { get; set; }
public string music_genre_vanity { get; set; }
}
public class MusicGenreList
{
public MusicGenre music_genre { get; set; }
}
public class PrimaryGenres
{
public List<MusicGenreList> music_genre_list { get; set; }
}
public class Track
{
public int track_id { get; set; }
public string track_name { get; set; }
public List<object> track_name_translation_list { get; set; }
public int track_rating { get; set; }
public int commontrack_id { get; set; }
public int instrumental { get; set; }
public int @explicit { get; set; }
public int has_lyrics { get; set; }
public int has_subtitles { get; set; }
public int has_richsync { get; set; }
public int num_favourite { get; set; }
public int album_id { get; set; }
public string album_name { get; set; }
public int artist_id { get; set; }
public string artist_name { get; set; }
public string track_share_url { get; set; }
public string track_edit_url { get; set; }
public int restricted { get; set; }
public DateTime updated_time { get; set; }
public PrimaryGenres primary_genres { get; set; }
}
public class TrackList
{
public Track track { get; set; }
}
public class Body
{
public List<TrackList> TrackList { get; set; }
}
public class Message
{
public Header header { get; set; }
public Body body { get; set; }
}
public class Root
{
public Message message { get; set; }
}
我尝试使用以下代码反序列化 JSON:
using (StreamReader r = new StreamReader(@"c:\users\xxxx\desktop.json"))
{
string json = r.ReadToEnd();
var tracks = JsonConvert.DeserializeObject<Track>(json);
}
但我一无所获。我是新来的;用更简单的 JSON 实现了它,但我无法弄清楚如何使用这段代码来实现它。我想打印一个只有歌曲名称的列表。 如果有人能帮助我,我将不胜感激!
这里有几个问题:
在您的
Body
class 中,TrackList
属性 与 JSON 不匹配。 JSON中对应的属性称为track_list
。 class 属性必须与 JSON 完全匹配(忽略大小写),否则您需要在 属性 上使用[JsonProperty]
属性来指示 JSON名字会。例如:public class Body { [JsonProperty("track_list")] public List<TrackList> TrackList { get; set; } }
您正在尝试反序列化为
Track
class,但您应该反序列化为Root
,因为它代表 [=41= 的根].var root = JsonConvert.DeserializeObject<Root>(json);
反序列化为
Root
后,您可以“向下钻取”以打印出轨道。foreach (var item in root.message.body.TrackList) { Console.WriteLine(item.track.track_name); }
Fiddle: https://dotnetfiddle.net/JnljGU