You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除JSON列表中特定空字段?Java代码失效排查

问题排查与解决方案

原始数据

parsedJsonChildData

[{date=null, feeForPerformance=null, feeFrequency=5}, {date=12-25-2024, feeForPerformance=23, feeFrequency=1}, {date=12-03-2025, feeForPerformance=1, feeFrequency=5}]

parsedXmlChildData

[{date=null, feeFrequency=5}, {date=12-25-2024, feeForPerformance=23, feeFrequency=1}, {date=12-03-2025, feeForPerformance=1, feeFrequency=5}]

需求规则

  • 若parsedJsonChildData中某个字段值为null,且parsedXmlChildData对应索引的对象中不存在该字段,则从parsedJsonChildData中移除该字段。
  • 示例:parsedJsonChildData第一个元素的feeForPerformance=null需被移除,因为parsedXmlChildData第一个元素无此字段。

问题代码

public static List<Map<String, Object>> cleanJsonData(List<Map<String, Object>> parsedJsonChildData, List<Map<String, Object>> parsedXmlChildData) {
    List<Map<String, Object>> modifiedParsedJsonChildData = new ArrayList<>();

    // Iterate over each index in the JSON and XML lists
    for (int i = 0; i < parsedJsonChildData.size(); i++) {
        Map<String, Object> jsonObject = parsedJsonChildData.get(i);
        Map<String, Object> xmlObject = parsedXmlChildData.get(i);

        // Create a new map to hold the cleaned JSON object
        Map<String, Object> cleanedJsonObject = new HashMap<>(jsonObject);

        // Iterate over the entries in the JSON object
        Iterator<Map.Entry<String, Object>> iterator = cleanedJsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String jsonField = entry.getKey();
            Object jsonValue = entry.getValue();

            // If the field is null in JSON and is missing in XML, remove it from JSON
            if (jsonValue == null && !xmlObject.containsKey(jsonField)) {
                iterator.remove(); // Remove the field from the copied JSON
                System.out.println("Removed field from JSON: " + jsonField); // Log the removal
            }
        }

        // Add the cleaned JSON object to the modified list
        modifiedParsedJsonChildData.add(cleanedJsonObject);
    }

    return modifiedParsedJsonChildData; // Return the cleaned JSON data only
}

执行结果

modifiedParsedJsonChildData (清理后): [{date=null, feeForPerformance=null, feeFrequency=5}, {date=12-25-2024, feeForPerformance=23, feeFrequency=1}, {date=12-03-2025, feeForPerformance=1, feeFrequency=5}] 

问题排查

1. 核心原因:方法返回值未被正确接收

你的代码逻辑本身是正确的,但调用方法时没有将返回的新列表赋值给目标变量,导致你看到的还是原始数据。比如错误的调用方式:

// 错误:仅调用方法,未接收返回值
cleanJsonData(parsedJsonChildData, parsedXmlChildData);

此时原始的parsedJsonChildData不会被修改,因为方法内部是创建新列表并返回,而非修改原列表。

2. 次要验证点:确认XML数据的实际结构

如果调用方式没问题,那需要检查parsedXmlChildData的第一个元素是否真的不包含feeForPerformance字段。若XML解析时将值为null的字段也存入了Map,那么xmlObject.containsKey("feeForPerformance")会返回true,导致移除条件不触发。


解决方案

方案1:正确接收方法返回值

调用方法时,将返回的清理后列表赋值给原变量或新变量:

// 正确:接收返回的清理后数据
parsedJsonChildData = cleanJsonData(parsedJsonChildData, parsedXmlChildData);

方案2:优化代码(可选)

如果希望直接修改原列表而非创建新列表,可以调整代码逻辑(但推荐返回新列表的方式,避免副作用):

public static void cleanJsonDataInPlace(List<Map<String, Object>> parsedJsonChildData, List<Map<String, Object>> parsedXmlChildData) {
    for (int i = 0; i < parsedJsonChildData.size(); i++) {
        Map<String, Object> jsonObject = parsedJsonChildData.get(i);
        Map<String, Object> xmlObject = parsedXmlChildData.get(i);

        Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            if (entry.getValue() == null && !xmlObject.containsKey(entry.getKey())) {
                iterator.remove();
                System.out.println("Removed field from JSON: " + entry.getKey());
            }
        }
    }
}

调用方式:

// 直接修改原列表
cleanJsonDataInPlace(parsedJsonChildData, parsedXmlChildData);

方案3:添加日志验证(排查用)

在代码中添加日志,确认每个判断条件的结果,比如:

// 在判断前添加日志
System.out.println("Checking field: " + jsonField);
System.out.println("JSON value is null: " + (jsonValue == null));
System.out.println("XML contains field: " + xmlObject.containsKey(jsonField));

通过日志可以明确看到条件是否满足,定位问题根源。


内容的提问来源于stack exchange,提问作者Beginercoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:41:03