服务器之家

服务器之家 > 正文

java xml转为json的n种方法

时间:2021-12-06 14:34     来源/作者:穆晟铭

java xml转为json的两种方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>";

先来看效果,效果一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
  "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"
        ]
      }
    ]
  }
}

效果二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "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
    }
  }
}

从效果来看,明显是第二种方法,比第一种好。

下面把代码贴出出来

第一种实现:用到的包是fastjson, jdom2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
        }
 
    }

第二种实现:用的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;)

?
1
2
3
4
5
6
7
8
9
10
11
<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>

实现方法简单:

?
1
2
3
4
5
6
7
org.json.JSONObject xmlJSONObj = null;
       try {
           xmlJSONObj = XML.toJSONObject(xml);
           log.debug("json:" + xmlJSONObj.toString() );
       } catch (JSONException e) {
           e.printStackTrace();
       }

到此这篇关于java xml转为json的两种方法的文章就介绍到这了,更多相关java xml转json内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/achengmu/p/15190645.html

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部