gpt4 book ai didi

Jackson XML to JSON 转换器删除多个子记录

转载 作者:行者123 更新时间:2023-12-03 14:29:58 24 4
gpt4 key购买 nike

我正在使用以下代码将我的源 XML 转换为 JSON。但是,此代码会删除源 XML 中任何多次出现的子记录,并且输出 JSON 仅包含最后一个子记录。

如何让 Jackson XML 到 JSON 转换器以 JSON 格式输出所有子记录?

代码

XmlMapper xmlMapper = new XmlMapper();
Map entries = xmlMapper.readValue(new File("source.xml"), LinkedHashMap.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writer().writeValueAsString(entries);
System.out.println(json);

源 XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
<NumLeases>1</NumLeases>
<FLAG>SUCCESS</FLAG>
<MESSAGE>Test Upload</MESSAGE>
<Lease>
<LeaseVersion>1</LeaseVersion>
<F1501B>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
</F1501B>
<F1501B>
<NEDOCO>18739</NEDOCO>
<NWUNIT>0005</NWUNIT>
<NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
</F1501B>
</Lease>
</File>

实际输出
{
"NumLeases": "1",
"FLAG": "SUCCESS",
"MESSAGE": "Test Upload",
"Lease": {
"LeaseVersion": "1",
"F1501B": {
"NEDOCO": "18739",
"NWUNIT": "0005",
"NTRUSTRECORDKEY": "8"
}
}
}

预期输出
{
"NumLeases": "1",
"FLAG": "SUCCESS",
"MESSAGE": "Test Upload",
"Lease": {
"LeaseVersion": "1",
"F1501B": [
{
"NEDOCO": "18738",
"NWUNIT": "0004",
"NTRUSTRECORDKEY": "12"
},
{
"NEDOCO": "18739",
"NWUNIT": "0005",
"NTRUSTRECORDKEY": "8"
}
]
}
}

任何帮助将不胜感激。谢谢!

最佳答案

我能够通过使用 org.json 来解决这个问题。 API 将源 XML 转换为 JSONObject然后通过 Jackson API 转换为 JSON。

代码

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

...
...

try (InputStream inputStream = new FileInputStream(new File(
"source.xml"))) {
String xml = IOUtils.toString(inputStream);
JSONObject jObject = XML.toJSONObject(xml);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Object json = mapper.readValue(jObject.toString(), Object.class);
String output = mapper.writeValueAsString(json);
System.out.println(output);
}

...
...

源 XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
<NumLeases>1</NumLeases>
<FLAG>SUCCESS</FLAG>
<MESSAGE>Test Upload</MESSAGE>
<Lease>
<LeaseVersion>1</LeaseVersion>
<F1501B>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
</F1501B>
<F1501B>
<NEDOCO>18739</NEDOCO>
<NWUNIT>0005</NWUNIT>
<NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
</F1501B>
</Lease>
</File>

输出
{
"File" : {
"NumLeases" : "1",
"FLAG" : "SUCCESS",
"MESSAGE" : "Test Upload",
"Lease" : {
"LeaseVersion" : "1",
"F1501B" : [ {
"NEDOCO" : "18738",
"NWUNIT" : "0004",
"NTRUSTRECORDKEY" : "12"
}, {
"NEDOCO" : "18739",
"NWUNIT" : "0005",
"NTRUSTRECORDKEY" : "8"
} ]
}
}
}

关于Jackson XML to JSON 转换器删除多个子记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com