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

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

[[\"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);
    }
}

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


感谢大家的收看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值