fastjson解析多层数据_FastJSON数据解析

本文通过实例代码展示了如何使用FastJSON解析包含多层嵌套数据的JSON字符串,包括获取时间、遍历JSON数组及对象、泛型反序列化和日期处理等常见操作,帮助开发者理解和应用FastJSON进行数据解析。
摘要由CSDN通过智能技术生成

先看个实际开发中的数据示例,解析如下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 it = plans.iterator();

List list = new ArrayList();

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 list = JSON.parseObject("...", new TypeReference>() {});

// 对日期类型的处理

// toJSONStringWithDateFormat可以处理各种Object类型(包括DateTime 和 LocalDateTime)

LocalDateTime date = LocalDateTime.now();

DateTime datetime = new DateTime();

Map 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

List class=JSON.parseArray((jsonString, Class.class);

// List

List listString = JSON.parseArray(jsonString, String.class);

// List< Map >

List> listMap = JSON.parseObject(jsonString, new TypeReference>>(){});

以上基本涵盖了开发中常用的数据解析,希望以上对小伙伴有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值