ASP.NET MVC中JSON强制小写属性名称

首先需要引用Newtonsoft.Json

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

然后转换对象

Model.JsonResult jsonResult = new Model.JsonResult
{
    State = 200,
    Message = "success",
    Data = dt.ToList()
};
string json = JsonConvert.SerializeObject(
    jsonResult,
    Formatting.Indented,
    new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    });

输出的JSON字符串

{
  "message": "success",
  "state": 200,
  "data": [],
  "script": ""
}
上一篇:DDoS攻击日益加剧,互联网企业该如何应对?


下一篇:ES6—44:Map数据结构