【 FastJSON 】解析多层嵌套

发布于:2025-06-18 ⋅ 阅读:(19) ⋅ 点赞:(0)

以下是使用 FastJSON 的 TypeReference 来解析该 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 类

// 设备信息类
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

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

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());
}

方法二:分步解析

// 第一步:解析为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 值,如果需要保留:

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

自定义字段名映射

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

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

4. 完整示例

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


网站公告

今日签到

点亮在社区的每一天
去签到