【Newtonsoft.Json】json序列化小駝峯格式(屬性名首字母小寫)

我是一名 ASP.NET 程序員,專一於 B/S 項目開發。累計文章閱讀量超過一千萬,個人博客主頁地址:https://www.itsvse.com/blog_xzz.htmlhtml

只須要設置JsonSerializerSettings參數便可。程序員

代碼以下:json

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

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

相關文章
相關標籤/搜索