gpt4 book ai didi

c# - 如何反序列化其中包含无效字段名称的 JSON 对象

转载 作者:可可西里 更新时间:2023-11-01 07:46:58 26 4
gpt4 key购买 nike

我有一个具有以下结构的 JSON 请求:

"formats":  {
"flash_embed": "http://a3.vikiassets.com/assets/vikiplayer-922746a667cfd38137a7e45df6ba1b95.swf?auto_play=true&language_codes=en&media_id=74965&partner=16&source=api_v3",
"m3u8": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965/ios.m3u8",
"res-150p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_mp4cell_150.mp4",
"res-240p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_240p.mp4",
"res-270p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_270p.mp4",
"res-360p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_360p.mp4",
"res-480p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_480p.mp4",
"res-720p": "http://wpc.354a.edgecastcdn.net/00354A/videos/encoded/74965_720p.mp4"
}
}

现在 res-150p,在 C# 中表示无效名称,如果我给它另一个名称,那么在去水化时我没有得到任何值,在 res-150p 中它是空的。

编辑:[可序列化]MoviesListRootObject 是包含 Response 的根对象,然后 Response 包含 Formats

public class MoviesListRootObject
{
public int count { get; set; }
public Pagination pagination { get; set; }
public List<Response> response { get; set; }
}

[Serializable]
public class Response
{
public int id { get; set; }
public int channel_id { get; set; }
public string title { get; set; }
public string title_language { get; set; }
public string description { get; set; }
public string description_language { get; set; }
public string created_at { get; set; }
public string uri { get; set; }
public string web_uri { get; set; }
public List<object> genres { get; set; }
public string origin_country { get; set; }
public string image { get; set; }
public Subtitles subtitles { get; set; }
public Formats formats { get; set; }
}

[Serializable]
public class Formats
{
public string flash_embed { get; set; }
public string m3u8 { get; set; }
public string __invalid_name__res150p { get; set; }
public string __invalid_name__res240p { get; set; }
public string __invalid_name__res270p { get; set; }
public string __invalid_name__res360p { get; set; }
public string __invalid_name__res480p { get; set; }
public string __invalid_name__res720p { get; set; }
public string __invalid_name__flv480p { get; set; }
public string __invalid_name__flv360p { get; set; }
public string __invalid_name__flv270p { get; set; }
public string __invalid_name__flvvp6360p { get; set; }
public string __invalid_name__flvvp6270p { get; set; }
}

最佳答案

您必须使用 JsonProperty 属性来修饰您的 Formats 属性,以在名称不完全匹配时告诉它去哪里:

partial class Formats
{
[JsonProperty("res-150p")]
public string __invalid_name__res150p {get; set;}
}

另见 Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

关于c# - 如何反序列化其中包含无效字段名称的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12215181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com