Xml转json
先导入需要的依赖包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
练习
package com.testfan.xml;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlTest {
public static void main(String[] args) {
XmlMapper xmlMapper = new XmlMapper();
ObjectMapper objectMapper = new ObjectMapper();
try {
String xml="<returnsms>" +
" <returnstatus>Success</returnstatus>" +
" <message>ok</message>" +
" <remainpoint>11032</remainpoint>" +
" <taskID>4354699</taskID>" +
" <test>123</test>" +
" <successCounts>1</successCounts>" +
"</returnsms>";
System.out.println(JSON.isValid(xml));
Map map =xmlMapper.readValue(xml, HashMap.class);
System.out.println(map);
System.out.println(JSON.toJSONString(map));
System.out.println(objectMapper.writeValueAsString(map));
} catch (IOException e) {
e.printStackTrace();
}
}
}