美文网首页
JSON字符串生成对应的实体类

JSON字符串生成对应的实体类

作者: 41uLove | 来源:发表于2018-05-25 22:12 被阅读0次

    一、需求场景

      最近进行接口开发和第三方接入,数据传输格式使用JSON,服务端将JSON数据序列化为对象使用,因此就要根据不同的接口来编写相应的参数对象,在对于请求或者响应达到几十上百字段的接口来说无疑是一件很恐怖的事情。根据开发文档编写参数及响应对象无疑是一件枯燥,繁琐,而且极易出现错误的事情。

    二、解决方案

       为了提高开发效率,完成日常工作计划(主要是为了偷懒🙄🙄 ),便向百度求助。发现了BeJSON一个工具网站,具有好多实用性功能,再次只分析JSON功能。

    {
        "animals":{
            "dog":[
                {
                    "name":"Rufus",
                    "breed":"labrador",
                    "count":1,
                    "twoFeet":false
                },
                {
                    "name":"Marty",
                    "breed":"whippet",
                    "count":1,
                    "twoFeet":false
                }
            ],
            "cat":{
                "name":"Matilda"
            }
        }
    }
    

      如上JSON转换为Java对象如下四个实体类:

    /**
      * Copyright 2018 bejson.com 
      */
    package com.besjon.pojo;
    
    /**
     * Auto-generated: 2018-05-25 22:32:55
     *
     * @author bejson.com (i@bejson.com)
     * @website http://www.bejson.com/java2pojo/
     */
    public class JsonRootBean {
        private Animals animals;
        public void setAnimals(Animals animals) {
             this.animals = animals;
         }
         public Animals getAnimals() {
             return animals;
         }
    }
    
    /**
      * Copyright 2018 bejson.com 
      */
    package com.besjon.pojo;
    
    /**
     * Auto-generated: 2018-05-25 22:32:55
     *
     * @author bejson.com (i@bejson.com)
     * @website http://www.bejson.com/java2pojo/
     */
    public class Dog {
        private String name;
        private String breed;
        private int count;
        private boolean twoFeet;
        public void setName(String name) {
             this.name = name;
         }
         public String getName() {
             return name;
         }
        public void setBreed(String breed) {
             this.breed = breed;
         }
         public String getBreed() {
             return breed;
         }
        public void setCount(int count) {
             this.count = count;
         }
         public int getCount() {
             return count;
         }
        public void setTwoFeet(boolean twoFeet) {
             this.twoFeet = twoFeet;
         }
         public boolean getTwoFeet() {
             return twoFeet;
         }
    }
    
    /**
      * Copyright 2018 bejson.com 
      */
    package com.besjon.pojo;
    
    /**
     * Auto-generated: 2018-05-25 22:32:55
     *
     * @author bejson.com (i@bejson.com)
     * @website http://www.bejson.com/java2pojo/
     */
    public class Cat {
        private String name;
        public void setName(String name) {
             this.name = name;
         }
         public String getName() {
             return name;
         }
    }
    /**
      * Copyright 2018 bejson.com 
      */
    package com.besjon.pojo;
    import java.util.List;
    
    /**
     * Auto-generated: 2018-05-25 22:32:55
     *
     * @author bejson.com (i@bejson.com)
     * @website http://www.bejson.com/java2pojo/
     */
    public class Animals {
    
        private List<Dog> dog;
        private Cat cat;
        public void setDog(List<Dog> dog) {
             this.dog = dog;
         }
         public List<Dog> getDog() {
             return dog;
         }
        public void setCat(Cat cat) {
             this.cat = cat;
         }
         public Cat getCat() {
             return cat;
         }
    }
    

      分析如上实体类,并不一定能满足我们的需求,而且此功能只能解析标准的 JSON。而我们的文档中大多是不标准的JSON如下:

    {
        "code": 0,
        "msg": "success",
        "data": {
            "city_list": [             //搜索结果
                {
                    "id": "1000001",        //城市分贝id
                    "name": "北京市",           //城市名称
                    "pinyin": "bei jing shi",           //城市拼音
                    "description": "北京市",            //城市说明
                    "cr_code": "CN",           //iso-3166 地区码
                    "cr_name": "中国",           //iso-3166 地区名称
                    "city_port_code": "PEK",            //城市机场三字码(航班列表,详情查询使用此码)
                    "station_list": [                   //城市机场列表
                        {
                            "name": "首都国际机场",         //机场名称
                            "code": "PEK"                   //机场三字码
                        },
                        {
                            "name": "南苑机场",
                            "code": "NAY"
                        }
                    ]
                }
            ],
            "hot_city_list": [        //热门城市列表
                {
                    "id": "1000001",
                    "name": "北京市",
                    "pinyin": "bei jing shi",
                    "description": "北京市",
                    "cr_code": "CN",            
                    "cr_name": "中国",   
                    "city_port_code": "PEK",
                    "station_list": [
                        {
                            "name": "首都国际机场",
                            "code": "PEK"
                        },
                        {
                            "name": "南苑机场",
                            "code": "NAY"
                        }
                    ]
                }
            ]
        }
    }
    

      这种JSON中充满了注释。而我们要是用此方法解析就必须删掉注释,解析成实体类之后再把注释填在实体类注释上,还有一点就是这个功能并不能将下划线转换成驼峰标识,在JAVA中变量使用下划线违背了JAVA命名规则,很显然这并不能满足我们的需求,因此便考虑自己实现一个解析这种非标准格式的JSON数据。
      直接上效果:

    package;
    
    import java.util.List;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class JSONConvertorTest{
        /**
         * success
         */
        @JsonProperty("msg")
        private String msg;
        /**
         * 0
         */
        @JsonProperty("code")
        private Integer code;
        /**
         * 
         */
        @JsonProperty("data")
        private Data data;
    
        public String getMsg(){
            return msg;
        }
    
        public void setMsg(String msg){
            this.msg = msg;
        }
    
        public Integer getCode(){
            return code;
        }
    
        public void setCode(Integer code){
            this.code = code;
        }
    
        public Data getData(){
            return data;
        }
    
        public void setData(Data data){
            this.data = data;
        }
        @Override
        public String toString() {
            return "JSONConvertorTest{" +
                "msg='" + msg + '\'' +
                "code='" + code + '\'' +
                "data='" + data + '\'' +
                '}';
        }
        public static class Data{
            /**
             * //热门城市列表
             */
            @JsonProperty("hot_city_list")
            private List<HotCityList> hotCityList;
            /**
             * //搜索结果
             */
            @JsonProperty("city_list")
            private List<CityList> cityList;
    
            public List<HotCityList> getHotCityList(){
                return hotCityList;
            }
            public void setHotCityList(List<HotCityList> hotCityList){
                this.hotCityList = hotCityList;
            }
    
            public List<CityList> getCityList(){
                return cityList;
            }
            public void setCityList(List<CityList> cityList){
                this.cityList = cityList;
            }
            @Override
            public String toString() {
                return "Data{" +
                    "hotCityList='" + hotCityList + '\'' +
                    "cityList='" + cityList + '\'' +
                    '}';
            }
        }
        public static class HotCityList{
            /**
             * bei jing shi
             */
            @JsonProperty("pinyin")
            private String pinyin;
            /**
             * 中国
             */
            @JsonProperty("cr_name")
            private String crName;
            /**
             * CN
             */
            @JsonProperty("cr_code")
            private String crCode;
            /**
             * PEK
             */
            @JsonProperty("city_port_code")
            private String cityPortCode;
            /**
             * 北京市
             */
            @JsonProperty("name")
            private String name;
            /**
             * 北京市
             */
            @JsonProperty("description")
            private String description;
            /**
             * 1000001
             */
            @JsonProperty("id")
            private String id;
            /**
             * //城市机场列表
             */
            @JsonProperty("station_list")
            private List<StationList> stationList;
    
            public String getPinyin(){
                return pinyin;
            }
            public void setPinyin(String pinyin){
                this.pinyin = pinyin;
            }
    
            public String getCrName(){
                return crName;
            }
            public void setCrName(String crName){
                this.crName = crName;
            }
    
            public String getCrCode(){
                return crCode;
            }
            public void setCrCode(String crCode){
                this.crCode = crCode;
            }
    
            public String getCityPortCode(){
                return cityPortCode;
            }
            public void setCityPortCode(String cityPortCode){
                this.cityPortCode = cityPortCode;
            }
    
            public String getName(){
                return name;
            }
            public void setName(String name){
                this.name = name;
            }
    
            public String getDescription(){
                return description;
            }
            public void setDescription(String description){
                this.description = description;
            }
    
            public String getId(){
                return id;
            }
            public void setId(String id){
                this.id = id;
            }
    
            public List<StationList> getStationList(){
                return stationList;
            }
            public void setStationList(List<StationList> stationList){
                this.stationList = stationList;
            }
            @Override
            public String toString() {
                return "HotCityList{" +
                    "pinyin='" + pinyin + '\'' +
                    "crName='" + crName + '\'' +
                    "crCode='" + crCode + '\'' +
                    "cityPortCode='" + cityPortCode + '\'' +
                    "name='" + name + '\'' +
                    "description='" + description + '\'' +
                    "id='" + id + '\'' +
                    "stationList='" + stationList + '\'' +
                    '}';
            }
        }
        public static class StationList{
            /**
             * PEK
             */
            @JsonProperty("code")
            private String code;
            /**
             * 首都国际机场
             */
            @JsonProperty("name")
            private String name;
    
            public String getCode(){
                return code;
            }
            public void setCode(String code){
                this.code = code;
            }
    
            public String getName(){
                return name;
            }
            public void setName(String name){
                this.name = name;
            }
            @Override
            public String toString() {
                return "StationList{" +
                    "code='" + code + '\'' +
                    "name='" + name + '\'' +
                    '}';
            }
        }
        public static class CityList{
            /**
             * bei jing shi,城市拼音
             */
            @JsonProperty("pinyin")
            private String pinyin;
            /**
             * 中国,iso-3166 地区名称
             */
            @JsonProperty("cr_name")
            private String crName;
            /**
             * CN,iso-3166 地区码
             */
            @JsonProperty("cr_code")
            private String crCode;
            /**
             * PEK,城市机场三字码(航班列表,详情查询使用此码)
             */
            @JsonProperty("city_port_code")
            private String cityPortCode;
            /**
             * 北京市,城市名称
             */
            @JsonProperty("name")
            private String name;
            /**
             * 北京市,城市说明
             */
            @JsonProperty("description")
            private String description;
            /**
             * 1000001,城市分贝id
             */
            @JsonProperty("id")
            private String id;
            /**
             * //城市机场列表
             */
            @JsonProperty("station_list")
            private List<StationList> stationList;
    
            public String getPinyin(){
                return pinyin;
            }
            public void setPinyin(String pinyin){
                this.pinyin = pinyin;
            }
    
            public String getCrName(){
                return crName;
            }
            public void setCrName(String crName){
                this.crName = crName;
            }
    
            public String getCrCode(){
                return crCode;
            }
            public void setCrCode(String crCode){
                this.crCode = crCode;
            }
    
            public String getCityPortCode(){
                return cityPortCode;
            }
            public void setCityPortCode(String cityPortCode){
                this.cityPortCode = cityPortCode;
            }
    
            public String getName(){
                return name;
            }
            public void setName(String name){
                this.name = name;
            }
    
            public String getDescription(){
                return description;
            }
            public void setDescription(String description){
                this.description = description;
            }
    
            public String getId(){
                return id;
            }
            public void setId(String id){
                this.id = id;
            }
    
            public List<StationList> getStationList(){
                return stationList;
            }
            public void setStationList(List<StationList> stationList){
                this.stationList = stationList;
            }
            @Override
            public String toString() {
                return "CityList{" +
                    "pinyin='" + pinyin + '\'' +
                    "crName='" + crName + '\'' +
                    "crCode='" + crCode + '\'' +
                    "cityPortCode='" + cityPortCode + '\'' +
                    "name='" + name + '\'' +
                    "description='" + description + '\'' +
                    "id='" + id + '\'' +
                    "stationList='" + stationList + '\'' +
                    '}';
            }
        }
    }
    

    如上便是解析结果,与BeJSON的不同点:

    • 解析注释,将// xxx注释与value组成了java字段的注释。
    • 将一段 JSON生成了一个主类,其余的以内部静态类存在。
    • 添加了toString方法。
    • 自动推断类型,判断StringList<String>,Integer,BigDecimal
    • 遵循Java命名规范,将下划线转驼峰。
    • 增加@JsonProperty注解,映射文档字段。

    三、项目介绍

    相关文章

      网友评论

          本文标题:JSON字符串生成对应的实体类

          本文链接:https://www.haomeiwen.com/subject/nuxpjftx.html