我有一种情况,xml字符串需要转换为json字符串,代码如下。列表未转换为JSON中的列表,最后一项仅出现在输出中。
代码:
public static void main(String[] args) throws Exception {
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree("<Find Status=\"Success\"><Result><Provider><lastUpdated>1545391251168</lastUpdated><connection><entityType>CIMHTTPGETRequest</entityType><entityId>M7IH6HVWAAAAU74VJ5F5PBJZ</entityId></connection><connection><entityType>CIMHTTPGETRequest</entityType><entityId>M7IH6HWBAAAAU74VJ7KW72FB</entityId></connection><connection><entityType>CIMHTTPGETRequest</entityType><entityId>M7IH6HWCAAAAU74VJ4TQATY4</entityId></connection></Provider></Result></Find>".getBytes());
ObjectMapper jsonMapper = new ObjectMapper();
System.out.println(jsonMapper.writeValueAsString(node));
}
实际输出为:
{“状态”:“成功”,“结果”:{“提供者”:{“ lastUpdated”:“ 1545391251168”,“连接”:{“ entityType”:“ CIMHTTPGETRequest”,“ entityId”:“ M7IH6HWCAAAAU74VJ4TQATY4” }}}}
预期输出为:
{“状态”:“成功”,“结果”:{“提供商”:{“ lastUpdated”:“ 1545391251168”,“连接”:[{“ entityType”:“ CIMHTTPGETRequest”,“ entityId”:“ M7IH6HVWAAAAU74VJ5F5PBJZ “},{” entityType“:” CIMHTTPGETRequest“,” entityId“:” M7IH6HWBAAAAU74VJ7KW72FB“},{” entityType“:” CIMHTTPGETRequest“,” entityId“:” M7IH6HWCAAAAU74VJ4TQATY4“}}}}
依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>