我使用以下代码将源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"
}
]
}
}
任何帮助都应该受到赞赏。谢谢!
答案 0 :(得分:7)
我能够通过使用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"
} ]
}
}
}
答案 1 :(得分:0)
问题是XML没有区分&#34; Objects&#34; (在JSON术语中,即键/值对的集合)和&#34; Arrays&#34; (没有名称的有序值序列)。使用数据绑定(POJO)时,可以从Java类型确定特定XML元素的含义 - 如果Java属性是List
或数组,则必须是&#34;数组&#34 ;;否则&#34;对象&#34; - 但是当只告诉映射器将XML映射到java.util.Map
时,没有这样的区别,并且默认处理将基本上考虑所有XML元素来指示&#34;对象&#34;。而且因为&#34; Objects&#34;不能具有重复名称的属性,只保留其中一个值(最后一个)。
使用XmlMapper
没有简单的方法可以避免这种情况:您需要某种自定义处理。