美文网首页
ASP.NET MVC中JSON强制小写属性名称

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

作者: 段煜华 | 来源:发表于2019-07-14 19:04 被阅读0次

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

相关文章

网友评论

      本文标题:ASP.NET MVC中JSON强制小写属性名称

      本文链接:https://www.haomeiwen.com/subject/fkyikctx.html