Java 对象、JSON字符串、Map、List转换

2020-04-03  本文已影响0人  邮差在行动

网上大部分的转换都使用阿里的fastjson,这里使用com.fasterxml.jackson.databind.ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
String str = objectMapper.writeValueAsString(javaObj);
JavaType valueType = objectMapper.getTypeFactory()
    .constructParametricType(HashMap.class, String.class, Object.class);
HashMap<String, Object> map = objectMapper.readValue(str, valueType);
JavaType valueType = objectMapper.getTypeFactory()
     .constructParametricType(ArrayList.class, Person.class);
List<Person> list = objectMapper.readValue(str, valueType);
上一篇下一篇

猜你喜欢

热点阅读