parsedXmlChildData (исходные данные): [{date=null, FeFrequency=5}, {date=25-12-2024, FeForPerformance=23, FeFrequency=1}, {date=03-12-2025, FeForPerformance =1, FeFrequency=5}]
В первом индексе parsedJsonChildData FeForPerformance=null следует удалить, поскольку он недоступен в parsedXmlChildData
Я пытался создать код, но все равно получаю тот же результат
Код: Выделить всё
public static List cleanJsonData(List parsedJsonChildData, List parsedXmlChildData) {
List modifiedParsedJsonChildData = new ArrayList();
// Iterate over each index in the JSON and XML lists
for (int i = 0; i < parsedJsonChildData.size(); i++) {
Map jsonObject = parsedJsonChildData.get(i);
Map xmlObject = parsedXmlChildData.get(i);
// Create a new map to hold the cleaned JSON object
Map cleanedJsonObject = new HashMap(jsonObject);
// Iterate over the entries in the JSON object
Iterator iterator = cleanedJsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry 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, FeForPerformance=null, FeFrequency=5}, {date=12 -25-2024,feForPerformance=23,feFrequency=1}, {date=12-03-2025, FeForPerformance=1, FeFrequency=5}]
все то же самое
Подробнее здесь: https://stackoverflow.com/questions/792 ... ch-are-nul