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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

段邵华 2019-08-02 11:52:00 浏览148
展开阅读全文

首先需要引用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": ""
}

网友评论

登录后评论
0/500
评论
段邵华
+ 关注