Json、Xml格式相互转换

1,234 阅读1分钟

1.导入依赖包

mvnrepository.com/artifact/or…

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20210307</version>
</dependency>

2.Xml转换为Json

package com.example;

import org.json.JSONObject;
import org.json.XML;

/**
 * com.example.excel
 * Description:
 *
 * @author jack
 * @date 2021/7/5 8:18 下午
 */
public class Xml2Json {
    public static void main(String[] args) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                "<MoBaoAccount MessageType=\"UserMobilePay\" PlatformID=\"b2ctest\">\n" +
                "  <OrderNo>M20150521084825</OrderNo>\n" +
                "  <TradeAmt>5000.00</TradeAmt>\n" +
                "  <Commission>0.5</Commission>\n" +
                "  <UserID>xiaolong</UserID>\n" +
                "  <MerchID>xiaolong1</MerchID>\n" +
                "  <tradeType>0</tradeType>\n" +
                "  <CustParam>123</CustParam>\n" +
                "  <NotifyUrl>http://mobaopay.com/callback.do</NotifyUrl>\n" +
                "  <TradeSummary>订单</TradeSummary>\n" +
                "</MoBaoAccount>";

        // XML转换为JSON
        JSONObject jsonObject = XML.toJSONObject(xml);
        System.out.println(jsonObject.toString());
    }
}

原始XML

<?xml version="1.0" encoding="utf-8" ?>
<MoBaoAccount MessageType="UserMobilePay" PlatformID="b2ctest">
  <OrderNo>M20150521084825</OrderNo>
  <TradeAmt>5000.00</TradeAmt>
  <Commission>0.5</Commission>
  <UserID>xiaolong</UserID>
  <MerchID>xiaolong1</MerchID>
  <tradeType>0</tradeType>
  <CustParam>123</CustParam>
  <NotifyUrl>http://mobaopay.com/callback.do</NotifyUrl>
  <TradeSummary>订单</TradeSummary>
</MoBaoAccount>

转换结果

{
    "MoBaoAccount":{
        "MerchID":"xiaolong1",
        "TradeSummary":"订单",
        "UserID":"xiaolong",
        "NotifyUrl":"http://mobaopay.com/callback.do",
        "PlatformID":"b2ctest",
        "Commission":0.5,
        "OrderNo":"M20150521084825",
        "TradeAmt":5000,
        "CustParam":123,
        "MessageType":"UserMobilePay",
        "tradeType":0
    }
}

3.Json转换为Xml

package com.example;

import org.json.JSONObject;
import org.json.XML;

/**
 * com.example.excel
 * Description:
 *
 * @author jack
 * @date 2021/7/5 8:18 下午
 */
public class Json2Xml {
    public static void main(String[] args) {
        String json = "{\n" +
                "    \"MoBaoAccount\":{\n" +
                "        \"MerchID\":\"xiaolong1\",\n" +
                "        \"TradeSummary\":\"订单\",\n" +
                "        \"UserID\":\"xiaolong\",\n" +
                "        \"NotifyUrl\":\"http://mobaopay.com/callback.do\",\n" +
                "        \"PlatformID\":\"b2ctest\",\n" +
                "        \"Commission\":0.5,\n" +
                "        \"OrderNo\":\"M20150521084825\",\n" +
                "        \"TradeAmt\":5000,\n" +
                "        \"CustParam\":123,\n" +
                "        \"MessageType\":\"UserMobilePay\",\n" +
                "        \"tradeType\":0\n" +
                "    }\n" +
                "}";

        // JSON转换为XML
        JSONObject jsonObject = new JSONObject(json);
        String json2Xml = "<?xml  version=\"1.0\" encoding=\"UTF-8\" ?>\n" + XML.toString(jsonObject);
        System.out.println(json2Xml);
    }
}

原始json

{
    "MoBaoAccount":{
        "MerchID":"xiaolong1",
        "TradeSummary":"订单",
        "UserID":"xiaolong",
        "NotifyUrl":"http://mobaopay.com/callback.do",
        "PlatformID":"b2ctest",
        "Commission":0.5,
        "OrderNo":"M20150521084825",
        "TradeAmt":5000,
        "CustParam":123,
        "MessageType":"UserMobilePay",
        "tradeType":0
    }
}

转换结果

<?xml  version="1.0" encoding="UTF-8" ?>
<MoBaoAccount>
    <MerchID>xiaolong1</MerchID>
    <TradeSummary>订单</TradeSummary>
    <UserID>xiaolong</UserID>
    <NotifyUrl>http://mobaopay.com/callback.do</NotifyUrl>
    <PlatformID>b2ctest</PlatformID>
    <Commission>0.5</Commission>
    <OrderNo>M20150521084825</OrderNo>
    <TradeAmt>5000</TradeAmt>
    <CustParam>123</CustParam>
    <MessageType>UserMobilePay</MessageType>
    <tradeType>0</tradeType>
</MoBaoAccount>