子进0672 2021-07-06 10:57 采纳率: 100%
浏览 47
已采纳

网关标准报文的拆组包配置

报文:
请求
{
"timeStamp": "时间戳",
"requestId": "请求标识",
"txBrNo": "机构编码",
"chnlCode": "渠道应用编码",
"serviceCode": "服务代码",
"svcSubCode": "服务细分编码",
"access_token": "token",
"sign": "签名",
"encryptAlgo": "加密算法",
"encodeKey": "加密算法密钥",
"version": "版本号",
"charset": "字符编码",
"requestData": {
"sysHead": {
"sourceSysTranDate": "发起系统交易日期",
"sourceSysTimeStamp": "发起系统交易时间",
"sourceSysCode": "发起方系统编码",
"glsTraceNo": "全局流水号",
"txMode": "交易模式",
"ttyId": "终端标识"
},
"body": {
"cstNo": "核心客户号"
}
}
}
响应:
{
"timeStamp": "时间戳",
"requestId": "请求标识",
"txBrNo": "机构编码",
"chnlCode": "渠道应用编码",
"serviceCode": "服务代码",
"svcSubCode": "服务细分编码",
"access_token": "token",
"sign": "签名",
"encryptAlgo": "加密算法",
"encodeKey": "加密算法密钥",
"version": "版本号",
"charset": "字符编码",
"responseData": {
"sysHead": {
"retCode": "交易返回码",
"retMsg": "交易返回信息",
"tranDate": "请求日期",
"tranTimeStamp": "请求时间"
},
"body": {
"cstNo": "核心客户号"
}
}
}

要求json转xml,xml转json的

  • 写回答

5条回答 默认 最新

  • ghostp 2021-07-06 20:52
    关注

    接上一个回答的补充,增加了jsonArray的处理

    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.StringReader;
    import java.io.StringWriter;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import org.xml.sax.SAXException;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * 
     * 〈XML和json相关转化〉<br/>
     * 〈包含主要入口类和相关测试〉<br/>
     * 〈此处使用了约定模式,json没有一个根节点的情况下,默认使用default_root,针对json的数组处理使用默认default_item〉
     *
     * @author ghostpbx
     * @see [相关类/方法](可选)
     * @since [产品/模块版本] (可选)
     */
    public class XmlJsonTrans {
    
        private static final String ENCODING_UTF8 = "UTF-8";
    
        private static final String DEFAULT_ROOT = "default_root";
    
        private static final String ITEM_NAME = "default_item";
    
        public static void main(String[] args) throws IOException, SAXException, DocumentException {
            String jsonStr = readFileContent("D:/devEnv/13-file/jsondemo.txt");
            JSONObject jobj = JSON.parseObject(jsonStr);
            System.out.println(json2Xml(jobj));
    
            String xmlStr = readFileContent("D:/devEnv/13-file/xmldemo3.txt");
            System.out.println(xml2Json(xmlStr));
        }
    
        
        /**
         * 
         * 功能描述: <br/>
         * 〈JSON对象转xml字符串〉
         *
         * @param json
         * @return
         * @throws SAXException
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        public static String json2Xml(JSONObject jsonObj) throws SAXException {
            return json2Doc(jsonObj).asXML();
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈JSON对象转Document对象〉
         *
         * @param jsonObj
         * @return
         * @throws SAXException
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static Document json2Doc(JSONObject jsonObj) throws SAXException {
            Document doc = DocumentHelper.createDocument();
            doc.setXMLEncoding(ENCODING_UTF8);
            if (jsonObj.keySet().size() > 1) {
                Element root = json2Element(jsonObj, DEFAULT_ROOT);
                doc.add(root);
            } else {
                for (String rootKey : jsonObj.keySet()) {
                    Object o = jsonObj.get(rootKey);
                    if (o instanceof JSONObject) {
                        Element root = json2Element(jsonObj.getJSONObject(rootKey), rootKey);
                        doc.add(root);
                    } else if (o instanceof JSONArray) {
    
                    }
                    break;
                }
            }
    
            return doc;
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈obj对象转化为Element〉
         *
         * @param obj
         * @param nodeName
         * @return
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static Element json2Element(Object obj, String nodeName) {
            if (obj instanceof JSONObject) {
                return jsonObj2Element((JSONObject) obj, nodeName);
            } else if (obj instanceof JSONArray) {
                return jsonArray2Element((JSONArray) obj, nodeName);
            }
            Element nodeEle = DocumentHelper.createElement(nodeName);
    
            return nodeEle;
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈JSON对象转Element对象〉
         *
         * @param jsonObj
         * @param nodeName
         * @return
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static Element jsonObj2Element(JSONObject jsonObj, String nodeName) {
            Element nodeEle = DocumentHelper.createElement(nodeName);
            for (String key : jsonObj.keySet()) {
                Object child = jsonObj.get(key);
                if (child instanceof JSONObject) {
                    nodeEle.add(jsonObj2Element(jsonObj.getJSONObject(key), key));
                } else if (child instanceof JSONArray) {
                    nodeEle.add(jsonArray2Element((JSONArray) child, key));
                } else {
                    Element element = DocumentHelper.createElement(key);
                    element.setText(jsonObj.getString(key));
                    nodeEle.add(element);
                }
            }
            return nodeEle;
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈json数组转化Element对象〉
         *
         * @param jsonArray
         * @param nodeName
         * @return
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static Element jsonArray2Element(JSONArray jsonArray, String nodeName) {
            Element nodeEle = DocumentHelper.createElement(nodeName);
            for (int i = 0; i < jsonArray.size(); i++) {
                Object obj = jsonArray.get(i);
                if (obj instanceof JSONObject) {
                    JSONObject jsonObj = (JSONObject) obj;
                    nodeEle.add(json2Element(jsonObj, ITEM_NAME));
                } else if (obj instanceof JSONArray) {
                    JSONArray childJsonArray = (JSONArray) obj;
                    nodeEle.add(jsonArray2Element(childJsonArray, ITEM_NAME));
                }
            }
            return nodeEle;
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈XML字符串转JSON对象〉
         *
         * @param xml
         * @return
         * @throws DocumentException
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        public static String xml2Json(String xml) throws DocumentException {
    
            SAXReader reader = new SAXReader();
            Document document = reader.read(new StringReader(xml));
            Element root = document.getRootElement();
    
            if (!DEFAULT_ROOT.equals(root.getName())) {
                JSONObject json = new JSONObject();
                json.put(root.getName(), element2Json(root));
                return json.toString();
            } else {
                return element2Json(root).toString();
            }
    
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈element转化为Object〉<br/>
         * 〈object为JSONObject和JSONArray对象〉
         * 
         *
         * @param element
         * @return
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static Object element2Json(Element element) {
    
            JSONObject json = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            boolean isArray = false;
            for (Object child : element.elements()) {
                Element e = (Element) child;
                if (ITEM_NAME.equals(e.getName())) {
    
                    jsonArray.add(element2Json(e));
                    isArray = true;
                } else {
                    if (e.elements().isEmpty()) {
                        json.put(e.getName(), e.getText());
                    } else {
                        json.put(e.getName(), element2Json(e));
                    }
                }
            }
    
            return isArray ? jsonArray : json;
        }
    
        /**
         * 
         * 功能描述: <br/>
         * 〈读取文件,为了能够读取数据方便使用测试〉
         *
         * @param fileName
         * @return
         * @throws IOException
         * @see [相关类/方法](可选)
         * @since [产品/模块版本](可选)
         */
        private static String readFileContent(String fileName) throws IOException {
            File file = new File(fileName);
            BufferedReader reader = null;
            StringBuffer sbf = new StringBuffer();
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    sbf.append(tempStr);
                }
                reader.close();
                return sbf.toString();
            } catch (IOException e) {
                throw e;
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                        throw e1;
                    }
                }
            }
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么