【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写)

我是一名 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}

【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写)_第1张图片

原文地址:https://down.itsvse.com/k/m4fdvk.html

你可能感兴趣的:(【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写))