我是一名 ASP.NET 程序员,专注于 B/S 项目开发。累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html
只需要设置JsonSerializerSettings参数即可。
代码如下:
public class TestModel
{
public string Name { get; set; }
public string Age { get; set; }
public string Url { get; set; } = "https://www.itsvse.com";
public int CustomId { get; set; }
}
static void Main(string[] args)
{
var model = new TestModel()
{
Name = "架构师",
Age = "5",
CustomId = 10086
};
var setting = new JsonSerializerSettings
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
};
var json = JsonConvert.SerializeObject(model, setting);
Console.WriteLine(json);
var json1 = JsonConvert.SerializeObject(model);
Console.WriteLine(json1);
Console.Read();
}
效果图如下:
{"name":"架构师","age":"5","url":"https://www.itsvse.com","customId":10086}
{"Name":"架构师","Age":"5","Url":"https://www.itsvse.com","CustomId":10086}