1. Import the dependency package

Mvnrepository.com/artifact/or…

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20210307</version>
</dependency>
Copy the code

2. Convert Xml to 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 = "
      \n" +
                "<MoBaoAccount MessageType=\"UserMobilePay\" PlatformID=\"b2ctest\">\n" +
                " 
      
       M20150521084825
      \n" +
                "< TradeAmt > 5000.00 < / TradeAmt > \ n" +
                "< appointed > 0.5 < / appointed > \ n" +
                " 
      
       xiaolong
      \n" +
                " 
      
       xiaolong1
      \n" +
                " 
      
       0
      \n" +
                " 
      
       123
      \n" +
                " 
      
       http://mobaopay.com/callback.do
      \n" +
                
      
        order 
      \n" +
                "</MoBaoAccount>";

        // Convert XML to JSONJSONObject jsonObject = XML.toJSONObject(xml); System.out.println(jsonObject.toString()); }}Copy the code

The original XML


      
<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>The order</TradeSummary>
</MoBaoAccount>
Copy the code

Conversion results

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

3. Convert Json to 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\":\" Order \,\n" +
                " \"UserID\":\"xiaolong\",\n" +
                " \"NotifyUrl\":\"http://mobaopay.com/callback.do\",\n" +
                " \"PlatformID\":\"b2ctest\",\n" +
                Appointed the \ '\' ": 0.5, \ n" +
                " \"OrderNo\":\"M20150521084825\",\n" +
                " \"TradeAmt\":5000,\n" +
                " \"CustParam\":123,\n" +
                " \"MessageType\":\"UserMobilePay\",\n" +
                " \"tradeType\":0\n" +
                " }\n" +
                "}";

        // convert JSON to XML
        JSONObject jsonObject = new JSONObject(json);
        String json2Xml = "
      \n"+ XML.toString(jsonObject); System.out.println(json2Xml); }}Copy the code

The original json

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

Conversion results


      
<MoBaoAccount>
    <MerchID>xiaolong1</MerchID>
    <TradeSummary>The order</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>
Copy the code