You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将指定SOAP响应转换为目标格式的JSON?

将SOAP响应转换为指定格式JSON的方法

现有如下SOAP响应内容:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:confirmPayer_10Response xmlns:ns2="http://v1.ws.provider.acp.com/">
         <return>
            <KeyValues>
               <Key>MICROFILM_NUMBER</Key>
               <Data>71111112236100141977490</Data>
            </KeyValues>
            <KeyValues>
               <Key>USER_TOKEN</Key>
               <Data>hpuser</Data>
            </KeyValues>
            <KeyValues>
               <Key>PASSWORD_TOKEN</Key>
               <Data>hpuser+22**</Data>
            </KeyValues>
         </return>
      </ns2:confirmPayer_10Response>
   </soap:Body>
</soap:Envelope>

需要将其转换为如下格式的JSON:

{
   "MICROFILM_NUMBER" : "71111112236100141977490",
   "USER_TOKEN" : "hpuser",
   "PASSWORD_TOKEN" : "hpuser+22**"
}

方法1:Python实现

利用Python内置的XML解析库提取节点数据,构建字典后转为JSON:

import xml.etree.ElementTree as ET
import json

# SOAP响应内容
soap_response = '''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:confirmPayer_10Response xmlns:ns2="http://v1.ws.provider.acp.com/">
         <return>
            <KeyValues>
               <Key>MICROFILM_NUMBER</Key>
               <Data>71111112236100141977490</Data>
            </KeyValues>
            <KeyValues>
               <Key>USER_TOKEN</Key>
               <Data>hpuser</Data>
            </KeyValues>
            <KeyValues>
               <Key>PASSWORD_TOKEN</Key>
               <Data>hpuser+22**</Data>
            </KeyValues>
         </return>
      </ns2:confirmPayer_10Response>
   </soap:Body>
</soap:Envelope>'''

# 解析XML
root = ET.fromstring(soap_response)
namespaces = {'ns2': 'http://v1.ws.provider.acp.com/'}

# 提取所有KeyValues节点
key_values = root.findall('.//ns2:return/KeyValues', namespaces)

# 构建目标字典
result = {}
for kv in key_values:
    result[kv.find('Key').text] = kv.find('Data').text

# 输出格式化JSON
print(json.dumps(result, indent=3))

方法2:JavaScript实现

通过浏览器或Node环境的DOMParser解析XML,遍历节点生成JSON:

const soapResponse = `
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:confirmPayer_10Response xmlns:ns2="http://v1.ws.provider.acp.com/">
         <return>
            <KeyValues>
               <Key>MICROFILM_NUMBER</Key>
               <Data>71111112236100141977490</Data>
            </KeyValues>
            <KeyValues>
               <Key>USER_TOKEN</Key>
               <Data>hpuser</Data>
            </KeyValues>
            <KeyValues>
               <Key>PASSWORD_TOKEN</Key>
               <Data>hpuser+22**</Data>
            </KeyValues>
         </return>
      </ns2:confirmPayer_10Response>
   </soap:Body>
</soap:Envelope>
`;

// 解析XML
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(soapResponse, "text/xml");

// 提取节点数据
const result = {};
xmlDoc.querySelectorAll('return KeyValues').forEach(node => {
    result[node.querySelector('Key').textContent] = node.querySelector('Data').textContent;
});

// 输出格式化JSON
console.log(JSON.stringify(result, null, 3));

方法3:离线工具转换

无需代码的话,可使用桌面端XML转JSON工具,导入SOAP内容后,设置映射规则:将每个KeyValues节点的Key值作为JSON键,Data值作为对应值,即可生成目标格式的JSON。

内容的提问来源于stack exchange,提问作者Aymen Kanzari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 16:15:39