当前位置: 代码网 > 科技>操作系统>Windows > 实体类与String相互转换实现方式

实体类与String相互转换实现方式

2026年04月28日 Windows 我要评论
实体类与string相互转换在工作中偶然遇到需要将实体类与string互转的情况,简单记录一下准备一个实体类@apimodel(description = "满减类型配置")@datapublic c

实体类与string相互转换

在工作中偶然遇到需要将实体类与string互转的情况,简单记录一下

准备一个实体类 

@apimodel(description = "满减类型配置")
@data
public class discounttypeconfig {
    /**
     * 满几件
     */
    @apimodelproperty(name = "full_goods_num", value = "满几件", required = true)
    @notnull(message = "满几件不能为空")
    @range(min = 1, message = "满几件必须大于0")
    private integer fullgoodsnum;

    /**
     * 打几折(百分比)
     */
    @apimodelproperty(name = "percentage_discount", value = "打几折(百分比)", required = true)
    @notnull(message = "打几折(百分比)不能为空")
    @range(min = 1, max = 99, message = "打几折(百分比)必须大于0小于100")
    private integer percentagediscount;

准备json工具类

package com.kaying.star.system.common.util.json;

import com.fasterxml.jackson.core.jsonprocessingexception;
import com.fasterxml.jackson.databind.javatype;
import com.fasterxml.jackson.databind.objectmapper;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;

import java.util.list;
import java.util.map;

/**
 * json工具类
 *
 */
@component
public class jsonutils {
    @autowired
    private objectmapper objectmapper;

    public string bean2json(object data) {
        try {
            string result = objectmapper.writevalueasstring(data);
            return result;
        } catch (jsonprocessingexception e) {
            e.printstacktrace();
        }
        return null;
    }

    public <t> t json2bean(string jsondata, class<t> beantype) {
        try {
            t result = objectmapper.readvalue(jsondata, beantype);
            return result;
        } catch (exception e) {
            e.printstacktrace();
        }
        return null;
    }

    public <t> list<t> json2list(string jsondata, class<t> beantype) {
        javatype javatype = objectmapper.gettypefactory().constructparametrictype(list.class, beantype);
        try {
            list<t> resultlist = objectmapper.readvalue(jsondata, javatype);
            return resultlist;
        } catch (exception e) {
            e.printstacktrace();
        }
        return null;
    }

    public <k, v> map<k, v> json2map(string jsondata, class<k> keytype, class<v> valuetype) {
        javatype javatype = objectmapper.gettypefactory().constructmaptype(map.class, keytype, valuetype);
        try {
            map<k, v> resultmap = objectmapper.readvalue(jsondata, javatype);
            return resultmap;
        } catch (exception e) {
            e.printstacktrace();
        }
        return null;
    }
}

转成string之后可以保存至数据库

string disconfig = jsonutils.bean2json(discounttypeconfig)

从数据库中取出string字段

将string转成实体类

//通过实体类转成的string 
string disconfig = jsonutils.bean2json(discounttypeconfig)

//通过工具类将string转换成实体类
discounttypeconfig discounttypeconfig = jsonutils.json2bean(disconfig , discounttypeconfig.class)

适用于各种不同的配置等使用场景,将实体类转成string存入数据库,将string取出转成实体类!

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com