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

    完美~~~

  • 相关阅读:
    黑客术语1
    leetcode笔记--3 Niim game
    台湾ML笔记--1.2 formalize the learning probelm
    台湾ML笔记--1.1什么时候适合使用ML
    leetcode笔记--2 reverse string
    leetcode笔记--1 two-sum
    数据挖掘导论笔记1
    python基础----ipython快捷键
    记录新的开始
    编译器错误消息: CS1617: 选项“6”对 /langversion 无效
  • 原文地址:https://www.cnblogs.com/jhli/p/9084300.html
Copyright © 2020-2023  润新知