Object Json字符串转List<Map<String, Object>>方式

1.利用JDK1.8

public List> objToListMap(Object obj) throws GlobalException {
    //1.先转为json数组    
    JSONArray jsonArr = JSONObject.parseArray(JSONObject.toJSONString(obj));
    List> listMapData = jsonArr.stream().
            map(s -> JSONObject.parseObject(JSONObject.toJSONString(s)).getInnerMap())
            .collect(Collectors.toList());
    return listMapData;
}

2.用jackson包

public List> objToListMap(Object obj) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonStr = JSONObject.toJSONString(obj);
    List> listMap = objectMapper.readValue(jsonStr, List.class);
    return listMap;
}

3.强制转换

public List> objToListMap(Object obj) {
    List> listMap =(List)JSONObject.parseArray(JSONObject.toJSONString(obj));
    return listMap;
}

你可能感兴趣的:(Spring,java,json,list,数据结构)