ASPNET-MVC中JSON强制小写属性名称

52次阅读

共计 400 个字符,预计需要花费 1 分钟才能阅读完成。

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