C# 如何反序列化包含无效字段名的JSON对象

C# 如何反序列化包含无效字段名的JSON对象,c#,.net,json,json.net,C#,.net,Json,Json.net,我有一个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:/

我有一个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
包含
格式

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; }
}
公共类MoviesListRootObject
{
公共整数计数{get;set;}
公共分页分页{get;set;}
公共列表响应{get;set;}
}
[可序列化]
公众课堂反应
{
公共int id{get;set;}
公共int通道_id{get;set;}
公共字符串标题{get;set;}
公共字符串title_语言{get;set;}
公共字符串说明{get;set;}
公共字符串描述\u语言{get;set;}
在{get;set;}处创建的公共字符串
公共字符串uri{get;set;}
公共字符串web_uri{get;set;}
公共列表类型{get;set;}
公共字符串来源国{get;set;}
公共字符串图像{get;set;}
公共字幕字幕字幕{get;set;}
公共格式格式{get;set;}
}
[可序列化]
公共类格式
{
公共字符串flash_嵌入{get;set;}
公共字符串m3u8{get;set;}
公共字符串uu无效u名称uu res150p{get;set;}
公共字符串\uuuu无效\uuuu名称\uuuures240p{get;set;}
公共字符串uu无效u名称uu res270p{get;set;}
公共字符串\uuu无效\u名称\uuuu res360p{get;set;}
公共字符串\uuu无效\u名称\uuuu res480p{get;set;}
公共字符串uu无效u名称uu res720p{get;set;}
公共字符串\uuuu无效\uuuu名称\uuuuFLV480P{get;set;}
公共字符串\uuuu无效\uuu名称\uuuuuFLV360P{get;set;}
公共字符串\uuuu无效\uuu名称\uuuuuFLV270P{get;set;}
公共字符串uuu无效uuuu flvvp6360p{get;set;}
公共字符串\uuuu无效\u名称\uuuuu flvvp6270p{get;set;}
}

您必须使用
JsonProperty
属性来修饰
格式
属性,以告诉它如果名称不完全匹配会发生什么:

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

另请参见如何反序列化它?使用类型var rootObj=JsonConvert.DeserializeObject(json)进行反序列化;什么是
MoviesListRootObject
?这就是失败的确切数据吗?@Ic,编辑了这个问题谢谢,我只是好奇地想知道,我也可以声明正常的比例,并使用属性使用不同的名称吗?@Simsons当然可以。这就是这里发生的事,对吗?碰巧您不能使用与JSON相同的名称,所以您必须使用新的名称(
\uuuu无效\uu name\uuuu res150p
)。