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

1.利用JDK1.8

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

2.用jackson包

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

3.强制转换

public List<Map<String, Object>> objToListMap(Object obj) {
    List<Map<String, Object>> listMap =(List)JSONObject.parseArray(JSONObject.toJSONString(obj));
    return listMap;
}
经验分享 程序员 微信小程序 职场和发展