【 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 来处理泛型类型的解析,确保嵌套的泛型结构能够正确映射。

相关推荐
linweidong9 分钟前
C++ 模块化编程(Modules)在大规模系统中的实践难点?
linux·前端·c++
leobertlan4 小时前
2025年终总结
前端·后端·程序员
子兮曰4 小时前
OpenClaw架构揭秘:178k stars的个人AI助手如何用Gateway模式统一控制12+通讯频道
前端·javascript·github
百锦再5 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
莲华君5 小时前
React快速上手:从零到项目实战
前端·reactjs教程
百锦再5 小时前
React编程高级主题:测试代码
android·前端·javascript·react.js·前端框架·reactjs
易安说AI5 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
JH30735 小时前
SpringBoot 优雅处理金额格式化:拦截器+自定义注解方案
java·spring boot·spring
Coder_Boy_6 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
失忆爆表症7 小时前
05_UI 组件库集成指南:Shadcn/ui + Tailwind CSS v4
前端·css·ui