MVC4 Web API返回带有特殊字符的json属性键字符串

本文关键字:json 属性 字符串 特殊字符 Web API 返回 MVC4 | 更新日期: 2023-09-27 18:18:35

c#不允许在属性名中使用空格等特殊字符。需要有这样一种方式来定义json对象。你知道吗?

{
   ".Class1 #Id1" : "Value1"
   ".Class1 #Id2" : "Value2"
}

MVC4 Web API返回带有特殊字符的json属性键字符串

尝试使用JsonProperty属性:

        public class MyClass
        {
            [JsonProperty(".Class1 #Id1")]
            public string id1 { get; set; }
            [JsonProperty(".Class1 #Id2")]
            public string id2 { get; set; }
        }