json serialization in small camel case format (the first letter of the attribute name is lowercase)

To quote Newtonsoft.Json, you only need to set the JsonSerializerSettings parameter.

using Newtonsoft.Json;
using System;

namespace ItemDemo
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            var model = new TestDTO()
            {
    
    
                Name = "小明",
                Age = "5",
                Address = "地球",
                Id = "10088",
                PairsOfWords="双单词"
            };
            var setting = new JsonSerializerSettings
            {
    
    
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("小驼峰格式(属性名首字母小写)");
            var json = JsonConvert.SerializeObject(model, setting);
            Console.WriteLine(json);
            Console.WriteLine();
            Console.WriteLine("驼峰格式");
            var json1 = JsonConvert.SerializeObject(model);
            Console.WriteLine(json1);
            Console.Read();
        }
     
    }
    public class TestDTO
    {
    
    
        public string Id {
    
     get; set; }

        public string Name {
    
     get; set; }

        public string Age {
    
     get; set; }

        public string Address {
    
     get; set; }
        public string PairsOfWords {
    
     get; set; }
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43972758/article/details/127627812