【 FastJSON 】解析多层嵌套

以下是使用 FastJSON 的 TypeReference 来解析该 JSON 数据的完整代码示例:

json 复制代码
{
	"msg": "操作成功",
	"code": 0,
	"data": {
		"total": 29,
		"rows": [{
			"deviceName": null,
			"deviceNo": "2303811032000001",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030002",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030001",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030003",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030004",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030005",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030006",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030009",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030008",
			"runningStatus": 1,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030011",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030010",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030014",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030013",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030016",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030015",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030017",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030018",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030020",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030019",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030023",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": "",
			"deviceNo": "2303812072030024",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": "",
			"y": ""
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030025",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030022",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030027",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030028",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030026",
			"runningStatus": 2,
			"onlineStatus": 1,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030012",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030007",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}, {
			"deviceName": null,
			"deviceNo": "2303812072030021",
			"runningStatus": 2,
			"onlineStatus": 2,
			"flow": "0",
			"sumWater": "0.0",
			"dayWater": "0.0",
			"dayTime": "0.0",
			"x": null,
			"y": null
		}]
	}
}

FastJSON 解析解决方案

根据您提供的 JSON 数据,我将展示如何使用 FastJSON 正确解析这种结构的数据。

1. 首先定义对应的 Java 类

java 复制代码
// 设备信息类
public class DeviceInfo {
    private String deviceName;
    private String deviceNo;
    private Integer runningStatus;
    private Integer onlineStatus;
    private String flow;
    private String sumWater;
    private String dayWater;
    private String dayTime;
    private String x;
    private String y;
    
    // getters and setters...
}

// 数据分页类
public class PageData<T> {
    private Integer total;
    private List<T> rows;
    
    // getters and setters...
}

// API响应类
public class ApiResponse<T> {
    private String msg;
    private Integer code;
    private T data;
    
    // getters and setters...
}

2. 使用 FastJSON 解析 JSON

方法一:直接解析完整结构

java 复制代码
String jsonString = "..."; // 您的JSON字符串

// 解析完整的响应结构
ApiResponse<PageData<DeviceInfo>> response = JSON.parseObject(
    jsonString, 
    new TypeReference<ApiResponse<PageData<DeviceInfo>>>() {}
);

// 使用数据
System.out.println("响应消息: " + response.getMsg());
System.out.println("总设备数: " + response.getData().getTotal());
for (DeviceInfo device : response.getData().getRows()) {
    System.out.println("设备编号: " + device.getDeviceNo());
    System.out.println("在线状态: " + device.getOnlineStatus());
}

方法二:分步解析

java 复制代码
// 第一步:解析为JSONObject
JSONObject jsonObject = JSON.parseObject(jsonString);

// 第二步:获取data部分
JSONObject dataObject = jsonObject.getJSONObject("data");

// 第三步:解析rows数组
List<DeviceInfo> deviceList = JSON.parseArray(
    dataObject.getString("rows"), 
    DeviceInfo.class
);

// 使用数据
for (DeviceInfo device : deviceList) {
    System.out.println("设备编号: " + device.getDeviceNo());
}

3. 处理可能的问题

处理 null 值

FastJSON 默认会忽略 null 值,如果需要保留:

java 复制代码
// 在解析时配置
Feature[] features = new Feature[]{
    Feature.AllowArbitraryCommas,
    Feature.IgnoreNotMatch,
    Feature.DisableCircularReferenceDetect
};
ApiResponse response = JSON.parseObject(jsonString, ApiResponse.class, features);

自定义字段名映射

如果 JSON 字段名与 Java 字段名不一致,可以使用 @JSONField 注解:

java 复制代码
public class DeviceInfo {
    @JSONField(name = "deviceNo")
    private String deviceId;
    
    // 其他字段...
}

4. 完整示例

java 复制代码
public class Main {
    public static void main(String[] args) {
        String jsonString = "..."; // 您的JSON字符串
        
        // 解析JSON
        ApiResponse<PageData<DeviceInfo>> response = JSON.parseObject(
            jsonString,
            new TypeReference<ApiResponse<PageData<DeviceInfo>>>() {}
        );
        
        // 输出结果
        System.out.println("响应消息: " + response.getMsg());
        System.out.println("响应代码: " + response.getCode());
        System.out.println("总记录数: " + response.getData().getTotal());
        
        System.out.println("\n设备列表:");
        for (DeviceInfo device : response.getData().getRows()) {
            System.out.printf("设备编号: %s, 在线状态: %d, 运行状态: %d%n",
                device.getDeviceNo(),
                device.getOnlineStatus(),
                device.getRunningStatus());
        }
    }
}

这样就能正确解析您提供的 JSON 数据结构了。关键点在于使用 TypeReference 来处理泛型类型的解析,确保嵌套的泛型结构能够正确映射。

相关推荐
Mh1 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
程序员大阳1 天前
副队长Vue3教程(2)--Vite 搭建 Vue 项目,认识目录结构
前端·javascript·vue.js·vite·创建项目·目录结构·项目结构
濮水大叔1 天前
Cabloy/Zova 与 Next.js 的 SSR 水合差异
前端·typescript·next.js
SoaringHeart1 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
Resean02231 天前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
小毛球.1 天前
第 3 篇:IDEA 最新版下载与完整安装教程(Mac)
java·macos·intellij-idea·idea
Flynt1 天前
Chrome给AI编程助手塞了203篇前端规范,我拆包实测:检索0.77秒是真快,扫描器有点挑食
前端·chrome·ai编程
北城笑笑1 天前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
Misnearch1 天前
ScheduledExecutorService
java·线程池
程序员韩星1 天前
OpenAI 兼容接口返回 404,先查请求地址,再查模型名
前端·人工智能·http