这里是使用alibba的feastJson,需要添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
对象转json字符串
String json = JSONObject.toJSONString(object);
json字符串转对象
public <T> T toObject(String json, Class<T> clazz) {
if(json == null) {
return null;
}
if(clazz == null) {
throw new DtvException("clazz不能为空");
}
try {
return JSON.parseObject(json, clazz);
} catch (Exception e) {
throw new DtvException(format(
"JSON字符串转换成对象失败, JSON:[{0}], class:[{1}]", json, clazz));
}
}
fastJson还有很多个方法 比如转成集合什么的 可以自己参考摸索一下