如何返回以数字开头的字段

时间:2017-06-01 11:22:50

标签: c# asp.net asp.net-web-api asp.net-core asp.net-core-webapi

我使用ASP.NET Core WebAPI并且我有方法调用应该返回以下数据:

 { "10v_module": 1, "core_module": 2 }

由于不可能有一个以数字开头的C#属性,我将如何做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:2)

就像是

    class Program
    {
        public static void Main()
        {
            string s = "{ \"10v_module\": 1, \"core_module\": 2 }";
            jsonData obj = JsonConvert.DeserializeObject<jsonData>(s);
        }
    }

    public class jsonData
    {
        [JsonProperty("10v_module")]
        public int v_module { get; set; }

        [JsonProperty("core_module")]
        public int core_module { get; set; }
    }

使用JSON.NET,您只需要添加JsonProperty属性并指定出现在结果JSON中的自定义名称

答案 1 :(得分:0)

您可以使用JsonPropertyAttribute为C#类的属性提供一个以数字开头的Json属性名称。

[JsonProperty("10v_module")]
public string Tenv_module {get;set;}
相关问题