- 先看个实际开发中的数据示例,解析如下JSON数据(JSON对象和数组的多层嵌套)
{
"deadline": "2020-12-29T16:00:00.000Z",
"content": [
{
"type": "edit",
"month": 12,
"plans": [
{
"planName": "12-1 天生我材必有用,千金散尽还复来。",
"planDeadline": "2020-12-15T01:40:19.179Z"
},
{
"planName": "12-2 烹羊宰牛且为乐,会须一饮三百杯。",
"planDeadline": "2020-12-31T01:40:28.044Z"
}
]
}
]
}
实际解析代码如下:
String str = "{\"deadline\":\"2020-12-30T16:00:00.000Z\",\"content\":[{\"type\":\"edit\",\"month\":12,\"plans\":[{\"planName\":\"【变更】12-1 天生我材必有用,千金散尽还复来。\",\"planDeadline\":\"2020-12-12T01:40:19.179Z\"},{\"planName\":\"【变更】12-2 烹羊宰牛且为乐,会须一饮三百杯。\",\"planDeadline\":\"2020-12-30T01:40:28.044Z\"}]}]}";
// 将str转换为JSONObject
JSONObject correctObj = JSON.parseObject(str);
// 获取时间并处理(可以使用hutool的方式)
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String nowStr = correctObj.getString("deadline");
String nowHandleStr = nowStr.substring(0, 10) + " " + nowStr.substring(11, 19);
LocalDateTime nowDeadline = LocalDateTime.parse(nowHandleStr, df);
// 获取JSONArray并进行遍历
JSONArray nowResult = correctObj.getJSONArray("content");
for (int i = 0; i < nowResult.size(); i++) {
JSONObject jsonObject = nowResult.getJSONObject(i);
String type = jsonObject.getString("type");
Integer month = jsonObject.getInteger("month");
JSONArray plans = jsonObject.getJSONArray("plans");
for (int j = 0; j < plans.size(); j++) {
JSONObject jsonPlan = plans.getJSONObject(j);
String planName = jsonPlan.getString("planName")
String planDeadline = jsonPlan.getString("planDeadline");
}
// i也可以使用Iterator方式进行比那里
Iterator<Object> it = plans.iterator();
List<JSONObject> list = new ArrayList<JSONObject>();
while (it.hasNext()) {
JSONObject plan = (JSONObject) it.next();
String planName = jsonObj.getString("planName");
String planDeadline = jsonPlan.getString("planDeadline");
// 整型数据 Integer num = (Integer) jsonObj.get("num");
}
}
- 下面对Fastjson的常用方法做一下归纳
// Object 转 String
String jsonString = JSON.toJSONString(group);
// String 转 Object
JSONObject obj = JSON.parseObject(str);
// String 转 实体类Group
String jsonString = ...;
Group group = JSON.parseObject(jsonString, Group.class);
// 泛型反序列化
import com.alibaba.fastjson.TypeReference;
List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});
// 对日期类型的处理
// toJSONStringWithDateFormat可以处理各种Object类型(包括DateTime 和 LocalDateTime)
LocalDateTime date = LocalDateTime.now();
DateTime datetime = new DateTime();
Map<String, Object> map = new HashMap<>();
map.put("bool", false);
map.put("deadline", date);
String mapStr = JSON.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss");
// 输出mapStr: {"bool":false,"deadline":"2020-11-26 19:49:29"}
- 解析JSON为四种常用类型
// JavaBean
Class class= JSON.parseObject(jsonString, Class.class);
// List<JavaBean>
List<Class> class=JSON.parseArray((jsonString, Class.class);
// List<String>
List<String> listString = JSON.parseArray(jsonString, String.class);
// List< Map<String,Object> >
List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});
以上基本涵盖了开发中常用的数据解析,希望以上对小伙伴有所帮助。