fastjson跟JackJson有很大区别,为了防止搞混,这里进行总结
因为会涉及到流水线的门禁,所以这里给出比较优质的解决方案
两个测试类代码如下:
@Data public class Student { private String name; private Integer age; private Teacher teacher; }
@Data public class Teacher { private String name; private Integer age; }
fastjson的一系列用法
json转类
如果json里面为空,也会正常转换
直接使用JSON.parseObject(json, Student.class);
类转json
String jsonString = JSON.toJSONString(student);
JSON转map
Map<String, Object> map = JSON.parseObject(jsonString, Map.class);
但是这么写会过不了流水线门禁,我们通常包一层,注意这里TypeReference最好使用fastjson的
private static final TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() {};
Map<String, Object> map3 = JSON.parseObject(jsonString, typeReference);
尤其注意,这里类Student中的类Teacher变成了JSONObject
jackson
类转Map
这跟fastjson不同,少了一步
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map2 = objectMapper.convertValue(student, Map.class);
要注意这里的map里面的内容全是linkedhashMap
标签:fastjson,Map,jackson,未写,JSON,private,json,class From: https://www.cnblogs.com/immersed-in-the-deep-sea/p/18073691