根据固定数据结构,没有key值封装成对象返回

news/2024/5/19 6:03:35 标签: 反射

一、前言
有些时候存储到数据库的时候是如下

[[\"35\",\"TSQ02-10001\",\"13\",\"基础型主机\",\"三重四级杆质谱系统\",\"TSQ Quantis\",50500.0,46000.0,42000.0],[\"36\",\"OPTON-32101\",\"21\",\"化学喷射装置\",\"大气压化学喷射装置\",\"APCI Source Kit\",1150.0,1050.0,950.0],[\"37\",\"OPTON-30666\",\"22\",\"启动包\",\"启动包\",\"Pre-Installation Kit\",110.0,100.0,90.0],[\"38\",\"OPTON-30626\",\"6\",\"软件\",\"食品安全方向定量软件\",\"TRACE FINDER general quan\",700.0,620.0,550.0],[\"39\",\"5400.0225\",\"13\",\"基础型主机\",\"UHPLC主机\",\"Vanquish Binary Flex w/o Detector\",9200.0,8300.0,7500.0],[\"40\",\"PEAK NM32LA\",\"19\",\"氮气发生器\",\"进口氮气发生器\",\"PEAK NM32LA\",4200.0,3800.0,3400.0],[\"41\",\"SANTAK\",\"20\",\"不间断电源\",\"1小时不间断电源\",\"UPS 10KVA\",730.0,660.0,600.0]]

仅仅只有value,但是所有的数据格式都是固定的,现在有一个需求,根据上述数据,封装到对应的类对象中,当然看当前数据,肯定是一个list返回

上述数据是由此类组成

public class ShowModularListBind {
    private String modularId;//模块编号
    private String cargoNum;//产品货号
    private String typeId;//模块类型编号
    private String typeName;//配件类型
    private String modularName;//配件名称
    private String modularSerie;//配件型号
    private Integer num;//数量
    private Integer sequence;//套餐模块的排序
    private Double threeMonth;//租期3月的月租金
    private Double sixMonth;//租期6月的月租金
    private Double nineMonth;//租期9月的月租金


    public String getModularId() {
        return modularId;
    }

    public void setModularId(String modularId) {
        this.modularId = modularId;
    }

    public String getCargoNum() {
        return cargoNum;
    }

    public void setCargoNum(String cargoNum) {
        this.cargoNum = cargoNum;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getModularName() {
        return modularName;
    }

    public void setModularName(String modularName) {
        this.modularName = modularName;
    }

    public String getModularSerie() {
        return modularSerie;
    }

    public void setModularSerie(String modularSerie) {
        this.modularSerie = modularSerie;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Double getThreeMonth() {
        return threeMonth;
    }

    public void setThreeMonth(Double threeMonth) {
        this.threeMonth = threeMonth;
    }

    public Double getSixMonth() {
        return sixMonth;
    }

    public void setSixMonth(Double sixMonth) {
        this.sixMonth = sixMonth;
    }

    public Double getNineMonth() {
        return nineMonth;
    }

    public void setNineMonth(Double nineMonth) {
        this.nineMonth = nineMonth;
    }

    public Integer getSequence() {
        return sequence;
    }

    public void setSequence(Integer sequence) {
        this.sequence = sequence;
    }

}

 

对应的抽象方法如下,思路【将上述数据变成key:value结构,再利用Gson去转换成对象】

-------------------------------pom-------------------------------------------

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.3.1</version>
</dependency>

------------------------------------方法--------------------------------------

import com.google.gson.Gson;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ASUS on 2018/5/29.
 */
public class DBStringToBean<T> {

    public <T> List<T> stringToBean(String dbStr, Class<T> clazz) {
        List<T> result = new ArrayList<T>();//返回数据结构封装

        /*--------------------------创建key数组-----------------------*/
        List<String> keyList = new LinkedList<>();
        Field[] fields = clazz.getDeclaredFields();//利用反射原理
        for (Field f : fields) {
            keyList.add(f.getName());
        }

        /*---------------------data部分-------------------------------*/
        String[] valueContentArray = null;
        List<String> dbStringLinkedList = new LinkedList<>();
        dbStr = dbStr.subSequence(dbStr.indexOf("[[") + 2, dbStr.lastIndexOf("]]")) + "";//去掉非法数据结构
        if (!StringUtils.isEmpty(dbStr)) {
            String[] strContentArray = dbStr.split("\\]\\,\\[");//组数再次分成分成list
            for (String strOne : strContentArray) {
                dbStringLinkedList.add(strOne);
            }
        }
        /*string和key组合*/
        try {
            StringBuffer buffer = null;

            for (int i = 0; i < dbStringLinkedList.size(); i++) {
                buffer = new StringBuffer();
                buffer.append("{");
                String stringGetIndexValList = dbStringLinkedList.get(i);
                valueContentArray = stringGetIndexValList.split(",");//list中取值
                for (int j = 0; j < valueContentArray.length; j++) {
                    if (j > 0) {
                        buffer.append(",");
                    }
                    String key = keyList.get(j);
                    buffer.append("\"" + key + "\":" + valueContentArray[j]);
                }
                buffer.append("}");
                T bean = parseJsonWithGson(buffer.toString(), clazz);
                result.add(bean);
            }

        } catch (Exception e) {

        }
        return result;

    }

    /**
     * @param jsonData
     * @param type
     * @param <T>
     * @return
     * @description 将Json一条【数据】解析成相应的映射对象---使用此需要导入gson.jar
     */
    public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
        Gson gson = new Gson();
        T obj = gson.fromJson(jsonData, type);
        return obj;
    }

    public static void main(String[] args) {
        String str = "[[\"35\",\"TSQ02-10001\",\"13\",\"基础型主机\",\"三重四级杆质谱系统\",\"TSQ Quantis\",50500.0,46000.0,42000.0],[\"36\",\"OPTON-32101\",\"21\",\"化学喷射装置\",\"大气压化学喷射装置\",\"APCI Source Kit\",1150.0,1050.0,950.0],[\"37\",\"OPTON-30666\",\"22\",\"启动包\",\"启动包\",\"Pre-Installation Kit\",110.0,100.0,90.0],[\"38\",\"OPTON-30626\",\"6\",\"软件\",\"食品安全方向定量软件\",\"TRACE FINDER general quan\",700.0,620.0,550.0],[\"39\",\"5400.0225\",\"13\",\"基础型主机\",\"UHPLC主机\",\"Vanquish Binary Flex w/o Detector\",9200.0,8300.0,7500.0],[\"40\",\"PEAK NM32LA\",\"19\",\"氮气发生器\",\"进口氮气发生器\",\"PEAK NM32LA\",4200.0,3800.0,3400.0],[\"41\",\"SANTAK\",\"20\",\"不间断电源\",\"1小时不间断电源\",\"UPS 10KVA\",730.0,660.0,600.0]]\n";
        List<ShowModularListBind> result = new DBStringToBean<ShowModularListBind>().stringToBean(str, ShowModularListBind.class);
        System.out.println(result);
    }
}

--------------------------------返回结果------------------------------------------


感谢大家的收看


http://www.niftyadmin.cn/n/1522111.html

相关文章

VS2010+QT 添加资源文件(.qrc)

1、项目右键菜单 &#xff0d; 添加 &#xff0d; 新键项 2、在弹出的“添加新项”对话框左侧的“类别”列表中选择 “Qt Resources” 3、在右侧的模板列表中选择 “Qt Resource” 4、输入资源文件名称。 Qt的窗体资源文件也是通过“新建项”添加的&#xff0c;过程和添加资源文…

没有key值,怎么将数据对应【接上一篇】

前言&#xff1a;昨天去了朋友公司写代码&#xff0c;他说发现一个问题&#xff0c;就是数据利用上一篇封装的类去对应数据&#xff0c;返回值是空&#xff0c;然后我说是因为超出边界&#xff0c;然后我查询除了一个问题&#xff0c;在使用Gson gson new Gson();的时候会将数…

html5 中錨点写法,html5中锚点的使用失去效果(用id代替以前的name),each遍历的时候,停留在第一个(序号为0),求帮助...

html>单页导航* {padding: 0;margin: 0;}a {text-decoration: none;color: #000;}.menu {width: 1050px;height: 40px;line-height: 40px;position: fixed;background-color: #ccc;left: 50%;margin-left: -525px;z-index: 1000;}.menu li{list-style-type: none;float: lef…

关于字符方式

1、单字节&#xff1a;ASSCII是一种单字节编码&#xff08;英文和数字&#xff09;&#xff0c;最多容纳256个字符。但是要处理中文显然一个字节是不够的&#xff0c;至少需要两个字节&#xff0c;而且还不能和ASCII编码冲突&#xff0c;所以&#xff0c;中国制定了GB2312编码。…

利用redis缓存,实现消息角标

前沿&#xff1a;类似微信的功能&#xff0c;用户在进入页面的时候可以看见未读信息的角标&#xff0c;点击之后角标消失。如下图所示 因为这个是首页模块&#xff0c;如果有大量的用户打开这个界面去DB查询的话&#xff0c;对于数据库的压力是十分之大的&#xff0c;现在下方实…

360实景地图插件 html,谷歌360全景地图让你足不出户欣赏美景

谷歌360全景地图让你足不出户欣赏美景随着科技的不断进步与发展&#xff0c;越来越多的先进技术出现在我们的生活当中。而网络地图也成为了众多先进技术之一&#xff0c;百度地图则成为了很多中国网名的出门必备工具&#xff0c;不过相比百度地图&#xff0c;谷歌360全景地图更…

一次配置 使用opencv库中的函数编写程序需要配置环境

原本以为每次都要配置&#xff0c;经过一楼提醒&#xff0c;原来是可以一下配置好的 哈哈&#xff0c;多谢。首先还是之前的设置&#xff0c;在末尾进行说明。 1.1 打开项目--属性--配置属性--VC目录&#xff0c;配置项目的包含目录和库目录&#xff0c;分别如下&#xff1a; …

自定义流程部署Activiti--部署篇【文章一】

前沿&#xff1a;有时候工作流需要实现用户自己画图&#xff0c;自己部署&#xff0c;自己启动&#xff0c;但是流程a.bpmn.xml这个东西一般都是程序员去画的也就是说&#xff0c;如果用户画图&#xff0c;怎么将界面简单化&#xff0c;这就是一个问题&#xff0c;如何将用户画…