【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/

 

完美~~~

 

posted @ 2018-05-24 18:04  0539  阅读(6158)  评论(0编辑  收藏  举报