【发布时间】:2018-11-14 07:01:09
【问题描述】:
我有一条 xml 消息需要转换为 json 消息格式。 xml 消息定义了包含元素类型信息的 xml 模式。但是,生成的 json 将所有值作为文本值而不是 xml 模式中提到的布尔值/数字。如何指示 XmlMapper() 从 xml 架构中派生类型信息?
XmlMapper xmlMapper = new XmlMapper();
String xmlMsg = getResourceContent("test.xml");
JsonNode node = xmlMapper.readTree(xmlMsg.getBytes());
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(node);
示例 xml:
<myMessage>
<id>333</id>
<type>Text</type>
<flag>true</flag>
</myMessage>
生成的 json:
{
"id": "333",
"type": "Text",
"flag": "true"
}
预期的 json:
{
"id": 333,
"type": "Text",
"flag": true
}
【问题讨论】:
标签: json xml jackson2 jackson-databind