java JSON List<bean> 转换成List<Map>

List 转换成List

目录

List 转换成List

1,将list实体类转换成list map

数据准备:

测试:

2,将listmap转换成list实体类

数据准备:

测试:

总结


 

在写代码的时候,遇到了要将List<实体类>改成List的情况,要怎么做呢?

这边主要用fastjson里面的JSON的内容,来进行转换

1,将list实体类转换成list map

方法

JSON.parseObject(JSONObject.toJSONString(map),T.class);

 

数据准备:

Shape

public class Shape implements Cloneable {
    private String color;
    private String type;
    public Shape(String color, String type) {
        this.color = color;
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    @Override
    public Object clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clone;
    }
    @Override
    public String toString() {
        return "Shape{" +
                "color='" + color + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}

 

ShapeFactory

public class ShapeFactory {
    private static final String COLORS[] =
            { "红色", "粉红色", "绿色", "蓝色", "棕色", "橘色" };
    private static final String SHAPES[] =
            { "圆形", "三角形", "长方形", "平行四边形", "扇形", "梯形" };
    public static List create(int size) {
        List shapeList = new ArrayList<>();
        // 先造一个原型
        Shape prototypeShape = new Shape(getRandomColor(), getRandomShape());
        for(int i=0; i < size; ++i) {
            Shape clone = (Shape) prototypeShape.clone();//复制原型
            clone.setColor(getRandomColor());
            clone.setType(getRandomShape());
            shapeList.add(clone);
        }
        return shapeList;
    }
    private static String getRandomColor() {
        return COLORS[(int)(Math.random()*COLORS.length)];
    }
    private static String getRandomShape() {
        return SHAPES[(int)(Math.random()*COLORS.length)];
    }
}

    使用原型模式的方式,动态生成一些测试数据

测试:

public static void main(String[] args) {
    changeBeanToListMap();
}

// 将list实体类转换成list map
public static void changeBeanToListMap() {
    List mapList=new ArrayList<>();
    List shapeList = ShapeFactory.create(10);
    System.out.println(shapeList.toString());
    System.out.println("========== change to Map ============");
    for(Object shape: shapeList){
        Map map = JSON.parseObject(JSONObject.toJSONString(shape), Map.class);
        mapList.add(map);
    }
    System.out.println(mapList.toString());
}

 

结果:

[Shape{color='绿色', type='圆形'}, Shape{color='粉红色', type='三角形'}, Shape{color='蓝色', type='梯形'}, Shape{color='红色', type='扇形'}, Shape{color='粉红色', type='三角形'}, Shape{color='红色', type='圆形'}, Shape{color='绿色', type='平行四边形'}, Shape{color='红色', type='梯形'}, Shape{color='粉红色', type='扇形'}, Shape{color='蓝色', type='圆形'}]

========== change to Map ============

[{color=绿色, type=圆形}, {color=粉红色, type=三角形}, {color=蓝色, type=梯形}, {color=红色, type=扇形}, {color=粉红色, type=三角形}, {color=红色, type=圆形}, {color=绿色, type=平行四边形}, {color=红色, type=梯形}, {color=粉红色, type=扇形}, {color=蓝色, type=圆形}]

 

2,将listmap转换成list实体类

方法

JSON.parseObject(JSONObject.toJSONString(shape), Map.class);

 

数据准备:

Shape 代码同上

 

ShapeListMapFactory

public class ShapeListMapFactory {
    private static final String COLORS[] =
            { "红色", "粉红色", "绿色", "蓝色", "棕色", "橘色" };
    private static final String SHAPES[] =
            { "圆形", "三角形", "长方形", "平行四边形", "扇形", "梯形" };
    public static List> create(int size) {
        List> shapeList = new ArrayList<>();
        for(int i=0; i < size; ++i) {
            Map single = new HashMap<>();
            single.put("color", getRandomColor());
            single.put("type", getRandomShape());
            shapeList.add(single);
        }
        return shapeList;
    }
    private static String getRandomColor() {
        return COLORS[(int)(Math.random()*COLORS.length)];
    }
    private static String getRandomShape() {
        return SHAPES[(int)(Math.random()*COLORS.length)];
    }
}

 

测试:

public static void main(String[] args) {
    changeListMapToBean();
}

// 将listmap转换成list实体类
public static void changeListMapToBean() {
    List shapeList=new ArrayList<>();
    List> dataList = ShapeListMapFactory.create(10);
    System.out.println(dataList.toString());
    System.out.println("========== change to Bean ============");
    Shape single;
    for(Object map: dataList){
        single= JSON.parseObject(JSONObject.toJSONString(map),Shape.class);
        shapeList.add(single);
    }
    System.out.println(shapeList.toString());
}

 

结果:

[{color=绿色, type=梯形}, {color=蓝色, type=扇形}, {color=橘色, type=长方形}, {color=棕色, type=平行四边形}, {color=蓝色, type=平行四边形}, {color=蓝色, type=圆形}, {color=蓝色, type=平行四边形}, {color=红色, type=梯形}, {color=绿色, type=平行四边形}, {color=棕色, type=长方形}]

========== change to Bean ============

[Shape{color='绿色', type='梯形'}, Shape{color='蓝色', type='扇形'}, Shape{color='橘色', type='长方形'}, Shape{color='棕色', type='平行四边形'}, Shape{color='蓝色', type='平行四边形'}, Shape{color='蓝色', type='圆形'}, Shape{color='蓝色', type='平行四边形'}, Shape{color='红色', type='梯形'}, Shape{color='绿色', type='平行四边形'}, Shape{color='棕色', type='长方形'}]

 

总结

   这边主要是使用fastjson里面的JSON来进行数据之间的转换,处理起来也比较简单,但是使用fastjson会存在一个问题,经常出现一些漏洞。如果项目里面没有fastjson,就得另寻它法了,需要使用BeanMap转为为Map的方式。

   两者的性能如何呢?

  

你可能感兴趣的:(java,java,bean,list,转换为map)