阿里开源的处理JSON的工具:
https://github.com/alibaba/fastjson
别人家的详细教程:
http://kimmking.github.io/2017/06/06/json-best-practice/
先在pom里添加上依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
判断String是否json
private boolean isJson(String s){
try {
JSONObject.parseObject(s);
return true;
} catch (Exception e) {
return false;
}
}
json和对象互转
//对象转json
String jsonString = JSON.toJSONString(user);
//json转对象
User u = JSON.parseObject(jsonString ,User.class);
//json转对象数组
List<User> userList = JSON.parseArray(jsonStringArray, User.class);