【JSON.NET】json序列化小駝峰格式(屬性名首字母小寫)


廢話少說,先上代碼

            var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };
            var json = JsonConvert.SerializeObject(resp, Formatting.None, setting);

 

直接序列化的效果如下

{
    "Status":1,
    "Message":"",
    "Detail":"",
    "Data":{
        "Count":6,
        "List":[
            {
                "Id":1,
                "ClassId":47933,
                "TeacherId":9429,
                "TeacherName":"高均",
                "LessonName":"聽力吸收"
            },
            {
                "Id":2,
                "ClassId":47933,
                "TeacherId":11211,
                "TeacherName":"毛金霞",
                "LessonName":"閱讀"
            },
            {
                "Id":3,
                "ClassId":47933,
                "TeacherId":10526,
                "TeacherName":"姜雨薇",
                "LessonName":"閱寫吸收"
            },
            {
                "Id":4,
                "ClassId":47933,
                "TeacherId":3330,
                "TeacherName":"吳燕",
                "LessonName":"寫作"
            },
            {
                "Id":5,
                "ClassId":47933,
                "TeacherId":6019,
                "TeacherName":"繆錦霞",
                "LessonName":"口語"
            },
            {
                "Id":6,
                "ClassId":47933,
                "TeacherId":9739,
                "TeacherName":"錢玉婷",
                "LessonName":"聽力"
            }
        ]
    }
}

 

 

加小駝峰效果如下

{
    "status":1,
    "message":"",
    "detail":"",
    "data":{
        "count":6,
        "list":[
            {
                "id":1,
                "classId":47933,
                "teacherId":6019,
                "teacherName":"繆錦霞",
                "lessonName":"口語"
            },
            {
                "id":2,
                "classId":47933,
                "teacherId":3330,
                "teacherName":"吳燕",
                "lessonName":"寫作"
            },
            {
                "id":3,
                "classId":47933,
                "teacherId":9739,
                "teacherName":"錢玉婷",
                "lessonName":"聽力"
            },
            {
                "id":4,
                "classId":47933,
                "teacherId":11211,
                "teacherName":"毛金霞",
                "lessonName":"閱讀"
            },
            {
                "id":5,
                "classId":47933,
                "teacherId":10526,
                "teacherName":"姜雨薇",
                "lessonName":"閱寫吸收"
            },
            {
                "id":6,
                "classId":47933,
                "teacherId":9429,
                "teacherName":"高均",
                "lessonName":"聽力吸收"
            }
        ]
    }
}

 

當然接口返回的是沒有格式化的json,為了節約網絡流量:

 

{"status":1,"message":"","detail":"","data":{"count":6,"list":[{"id":1,"classId":47933,"teacherId":6019,"teacherName":"繆錦霞","lessonName":"口語"},{"id":2,"classId":47933,"teacherId":3330,"teacherName":"吳燕","lessonName":"寫作"},{"id":3,"classId":47933,"teacherId":9739,"teacherName":"錢玉婷","lessonName":"聽力"},{"id":4,"classId":47933,"teacherId":11211,"teacherName":"毛金霞","lessonName":"閱讀"},{"id":5,"classId":47933,"teacherId":10526,"teacherName":"姜雨薇","lessonName":"閱寫吸收"},{"id":6,"classId":47933,"teacherId":9429,"teacherName":"高均","lessonName":"聽力吸收"}]}}

 

格式化工具,拿過去格式化一下就是上面的例子

https://www.json.cn/

 

完美~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM