自学内容网 自学内容网

【xml转JSON】

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 {
 //xml 格式数据
 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();
}

}

}


原文地址:https://blog.csdn.net/weixin_43186788/article/details/143529625

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!