java xml轉(zhuǎn)為json的n種方法
java xml轉(zhuǎn)為json的兩種方法
<?xml version="1.0" encoding="utf-8" ?> <auibinsurancecallback> <policyinfo> <transtype>TKTS</transtype> <eticketno>xxx</eticketno> <flightnumber>xxx</flightnumber> <flightdate>2019-10-16</flightdate> <operatetime>2019-10-16 17:20:00</operatetime> <insureno>1910161720056066735</insureno><agreeno>102160199</agreeno> <policyno> </policyno><policyurl> <!--[CDATA[]]--> </policyurl></policyinfo> <returninfo> <serialnumber>2019103015284949545354 </serialnumber> <retruncode>0</retruncode><errormessage> <!--[CDATA[xxx]]--> </errormessage> </returninfo> </auibinsurancecallback>";
先來看效果,效果一:
{
"auibinsurancecallback": {
"returninfo": [
{
"retruncode": [
"0"
],
"serialnumber": [
"2019103015284949545354"
]
}
],
"policyinfo": [
{
"operatetime": [
"2019-10-16 17:20:00"
],
"transtype": [
"TKTS"
],
"flightdate": [
"2019-10-16"
],
"insureno": [
"1910161720056066735"
],
"flightnumber": [
"xxx"
],
"agreeno": [
"102160199"
],
"eticketno": [
"xxxx"
]
}
]
}
}
效果二:
{
"auibinsurancecallback": {
"returninfo": {
"errormessage": "",
"retruncode": 0,
"serialnumber": 2.0191030152849496e+21
},
"policyinfo": {
"policyurl": "",
"operatetime": "2019-10-16 17:20:00",
"transtype": "TKTS",
"flightdate": "2019-10-16",
"insureno": 1910161720056066800,
"flightnumber": "xxx",
"agreeno": 102160199,
"policyno": "",
"eticketno": xxx
}
}
}
從效果來看,明顯是第二種方法,比第一種好。
下面把代碼貼出出來
第一種實現(xiàn):用到的包是fastjson, jdom2
public static JSONObject xml2JSON(byte[] xml) throws JDOMException, IOException {
JSONObject json = new JSONObject();
InputStream is = new ByteArrayInputStream(xml);
SAXBuilder sb = new SAXBuilder();
org.jdom2.Document doc = sb.build(is);
Element root = doc.getRootElement();
json.put(root.getName(), iterateElement(root));
return json;
}
private static JSONObject iterateElement(Element element) {
List node = element.getChildren();
Element et = null;
JSONObject obj = new JSONObject();
List list = null;
for (int i = 0; i < node.size(); i++) {
list = new LinkedList();
et = (Element) node.get(i);
if (et.getTextTrim().equals("")) {
if (et.getChildren().size() == 0)
continue;
if (obj.containsKey(et.getName())) {
list = (List) obj.get(et.getName());
}
list.add(iterateElement(et));
obj.put(et.getName(), list);
} else {
if (obj.containsKey(et.getName())) {
list = (List) obj.get(et.getName());
}
list.add(et.getTextTrim());
obj.put(et.getName(), list);
}
}
return obj;
}
@Test
public void xml1(){
String xml = 上面貼的xml;
JSONObject json= null;
try {
json = xml2JSON(xml.getBytes());
System.out.println(json.toJSONString());
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
第二種實現(xiàn):用的org.json包,
在用org.json包的時候,需要把spring-boot-starter-test中的,android-json排除,要不然會報錯:
java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/Reader;)V
java.lang.NoSuchMethodError: org.json.JSONObject.put(Ljava/lang/String;Ljava/util/Collection;)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
實現(xiàn)方法簡單:
org.json.JSONObject xmlJSONObj = null;
try {
xmlJSONObj = XML.toJSONObject(xml);
log.debug("json:" + xmlJSONObj.toString() );
} catch (JSONException e) {
e.printStackTrace();
}
到此這篇關(guān)于java xml轉(zhuǎn)為json的兩種方法的文章就介紹到這了,更多相關(guān)java xml轉(zhuǎn)json內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle
這篇文章主要介紹了mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
指定springboot的jar運(yùn)行內(nèi)存方式
這篇文章主要介紹了指定springboot的jar運(yùn)行內(nèi)存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringMVC中轉(zhuǎn)發(fā)與重定向的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于SpringMVC中轉(zhuǎn)發(fā)與重定向的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Spring+Quartz實現(xiàn)動態(tài)任務(wù)調(diào)度詳解
這篇文章主要介紹了Spring+Quartz實現(xiàn)動態(tài)任務(wù)調(diào)度詳解,最近經(jīng)常基于spring?boot寫定時任務(wù),并且是使用注解的方式進(jìn)行實現(xiàn),分成的方便將自己的類注入spring容器,需要的朋友可以參考下2024-01-01
Java8函數(shù)式接口的基礎(chǔ)學(xué)習(xí)教程
這篇文章主要給大家介紹了關(guān)于Java8函數(shù)式接口基礎(chǔ)學(xué)習(xí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

