自定义实体JSON转换

2 篇文章 0 订阅

java实现自定义实体进行JSON转换,最后输出内容

  • 本文自定义三层实体,以身份证json信息为例

    • 1:使用alibaba的fastjson包
    • 2:自定义内嵌字段实体
    • 3:JSON的内容转换,替换为跟实体相同字段名称
    • 4:替换之后进行JSON.parseObject()转换
  • 首先搭建好springboot项目,引入pom文件

    	<dependency>
    		<groupId>net.sf.json-lib</groupId>
    		<artifactId>json-lib</artifactId>
    		<version>2.2.3</version>
    		<classifier>jdk15</classifier><!-- 指定jdk版本 -->
    	</dependency>
    	<dependency>
    		<groupId>com.alibaba</groupId>
    		<artifactId>fastjson</artifactId>
    		<version>1.2.4</version>
    	</dependency>
    
  • 根据数据类型样式结构新建自定义实体

  • 下面是本次转换的json数据事例
    {
    “log_id” : 1354360663928995840,
    “words_result_num” : 6,
    “idcard_number_type” : 1,
    “image_status” : normal,
    “words_result”: {
    “住址”: {
    “location”: {
    “left”: 267,
    “top”: 453,
    “width”: 459,
    “height”: 99
    },
    “words”: “南京市江宁区弘景大道3889号”
    },
    “公民身份号码”: {
    “location”: {
    “left”: 443,
    “top”: 681,
    “width”: 589,
    “height”: 45
    },
    “words”: “330881199904173914”
    },
    “出生”: {
    “location”: {
    “left”: 270,
    “top”: 355,
    “width”: 357,
    “height”: 45
    },
    “words”: “19990417”
    },
    “姓名”: {
    “location”: {
    “left”: 267,
    “top”: 176,
    “width”: 152,
    “height”: 50
    },
    “words”: “伍云龙”
    },
    “性别”: {
    “location”: {
    “left”: 269,
    “top”: 262,
    “width”: 33,
    “height”: 52
    },
    “words”: “男”
    },
    “民族”: {
    “location”: {
    “left”: 492,
    “top”: 279,
    “width”: 30,
    “height”: 37
    },
    “words”: “汉”
    }
    },
    “words_result_num”: 6
    }

  • 新建实体TestEntity,新建的实体直接从words_result开始装袋

public class TestEntity {
    /**
     * words_result : {"姓名":{"words":"张三","location":{"top":495,"left":830,"width":76,"height":172}},"民族":{"words":"汉","location":{"top":798,"left":710,"width":61,"height":56}},"住址":{"words":"广东省廉江市三河镇李村57号","location":{"top":487,"left":382,"width":142,"height":643}},"公民身份号码":{"words":"480881199204191728","location":{"top":750,"left":157,"width":69,"height":806}},"出生":{"words":"19920419","location":{"top":496,"left":592,"width":58,"height":496}},"性别":{"words":"男","location":{"top":487,"left":708,"width":63,"height":60}}}
     * log_id : 1354360663928995840
     * words_result_num : 6
     * idcard_number_type : 1
     * image_status : normal
     */

    private WordsResultBean words_result;
    private Long log_id;
    private Integer words_result_num;
    private Integer idcard_number_type;
    private String image_status;
    private String risk_type;

    public WordsResultBean getWords_result() {
        return words_result;
    }

    public void setWords_result(WordsResultBean words_result) {
        this.words_result = words_result;
    }

    public Long getLog_id() {
        return log_id;
    }

    public void setLog_id(Long log_id) {
        this.log_id = log_id;
    }

    public Integer getWords_result_num() {
        return words_result_num;
    }

    public void setWords_result_num(Integer words_result_num) {
        this.words_result_num = words_result_num;
    }

    public Integer getIdcard_number_type() {
        return idcard_number_type;
    }

    public void setIdcard_number_type(Integer idcard_number_type) {
        this.idcard_number_type = idcard_number_type;
    }

    public String getImage_status() {
        return image_status;
    }

    public void setImage_status(String image_status) {
        this.image_status = image_status;
    }

    public String getRisk_type() {
        return risk_type;
    }

    public void setRisk_type(String risk_type) {
        this.risk_type = risk_type;
    }

    public static class WordsResultBean {
        /**
         * 姓名name : {"words":"张三","location":{"top":495,"left":830,"width":76,"height":172}}
         * 民族nation : {"words":"汉","location":{"top":798,"left":710,"width":61,"height":56}}
         * 住址address : {"words":"广东省廉江市三河镇李村57号","location":{"top":487,"left":382,"width":142,"height":643}}
         * 公民身份号码identityNumber : {"words":"480881199204191728","location":{"top":750,"left":157,"width":69,"height":806}}
         * 出生birth : {"words":"19920419","location":{"top":496,"left":592,"width":58,"height":496}}
         * 性别sex : {"words":"男","location":{"top":487,"left":708,"width":63,"height":60}}
         */

        private wordsBean name;
        private wordsBean nation;
        private wordsBean address;
        private wordsBean identityNumber;
        private wordsBean birth;
        private wordsBean sex;
        private wordsBean failure;
        private wordsBean organ;
        private wordsBean issuance;


        public wordsBean getName() {
            return name;
        }

        public void setName(wordsBean name) {
            this.name = name;
        }

        public wordsBean getNation() {
            return nation;
        }

        public void setNation(wordsBean nation) {
            this.nation = nation;
        }

        public wordsBean getAddress() {
            return address;
        }

        public void setAddress(wordsBean address) {
            this.address = address;
        }

        public wordsBean getIdentityNumber() {
            return identityNumber;
        }

        public void setIdentityNumber(wordsBean identityNumber) {
            this.identityNumber = identityNumber;
        }

        public wordsBean getBirth() {
            return birth;
        }

        public void setBirth(wordsBean birth) {
            this.birth = birth;
        }

        public wordsBean getSex() {
            return sex;
        }

        public void setSex(wordsBean sex) {
            this.sex = sex;
        }

        public wordsBean getFailure() {
            return failure;
        }

        public void setFailure(wordsBean failure) {
            this.failure = failure;
        }

        public wordsBean getOrgan() {
            return organ;
        }

        public void setOrgan(wordsBean organ) {
            this.organ = organ;
        }

        public wordsBean getIssuance() {
            return issuance;
        }

        public void setIssuance(wordsBean issuance) {
            this.issuance = issuance;
        }

        public static class wordsBean {
            public String getWords() {
                return words;
            }

            public void setWords(String words) {
                this.words = words;
            }

            public LocationBean getLocation() {
                return location;
            }

            public void setLocation(LocationBean location) {
                this.location = location;
            }

            /**
             * words : 张三
             * location : {"top":495,"left":830,"width":76,"height":172}
             */
            private String words;
            private LocationBean location;

        }


        public static class LocationBean {
            /**
             * top : 495
             * left : 830
             * width : 76
             * height : 172
             */
            private Integer top;
            private Integer left;
            private Integer width;
            private Integer height;

            public Integer getTop() {
                return top;
            }

            public void setTop(Integer top) {
                this.top = top;
            }

            public Integer getLeft() {
                return left;
            }

            public void setLeft(Integer left) {
                this.left = left;
            }

            public Integer getWidth() {
                return width;
            }

            public void setWidth(Integer width) {
                this.width = width;
            }

            public Integer getHeight() {
                return height;
            }

            public void setHeight(Integer height) {
                this.height = height;
            }
        }
    }
}

  • 进行中文与实体字段对象进行转换
    因为json是中文,进行转换使其匹配自定义实体类,此处非必须步骤,但是实体对象只能使用英文不能中文。
result = result.replaceAll("\"姓名\"", "\"name\"");
result = result.replaceAll("\"民族\"","\"nation\"");
result = result.replaceAll("\"住址\"","\"address\"");
result = result.replaceAll("\"公民身份号码\"","\"identityNumber\"");
result = result.replaceAll("\"出生\"","\"birth\"");
result = result.replaceAll("\"性别\"","\"sex\"");
result = result.replaceAll("\"失效日期\"","\"failure\"");
result = result.replaceAll("\"签发机关\"","\"organ\"");
result = result.replaceAll("\"签发日期\"","\"issuance\"");
  • 进行装袋,将json转换为实体中
TestEntity testEntity =JSON.parseObject(result, TestEntity.class);
  • 取实体数据方法
testEntity.getWords_result().getFailure().getWords();
testEntity.getImage_status();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dlei东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值