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

时间:2022-02-28 18:09:15

废话少说,先上代码

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

直接序列化的效果如下

{
"Status":,
"Message":"",
"Detail":"",
"Data":{
"Count":,
"List":[
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"高均",
"LessonName":"听力吸收"
},
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"毛金霞",
"LessonName":"阅读"
},
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"姜雨薇",
"LessonName":"阅写吸收"
},
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"吴燕",
"LessonName":"写作"
},
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"缪锦霞",
"LessonName":"口语"
},
{
"Id":,
"ClassId":,
"TeacherId":,
"TeacherName":"钱玉婷",
"LessonName":"听力"
}
]
}
}

加小驼峰效果如下

{
"status":,
"message":"",
"detail":"",
"data":{
"count":,
"list":[
{
"id":,
"classId":,
"teacherId":,
"teacherName":"缪锦霞",
"lessonName":"口语"
},
{
"id":,
"classId":,
"teacherId":,
"teacherName":"吴燕",
"lessonName":"写作"
},
{
"id":,
"classId":,
"teacherId":,
"teacherName":"钱玉婷",
"lessonName":"听力"
},
{
"id":,
"classId":,
"teacherId":,
"teacherName":"毛金霞",
"lessonName":"阅读"
},
{
"id":,
"classId":,
"teacherId":,
"teacherName":"姜雨薇",
"lessonName":"阅写吸收"
},
{
"id":,
"classId":,
"teacherId":,
"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/

完美~~~