0

我正在使用节点包 xml-js 将 SOAP 响应转换为 json。

下面详细介绍了我在两个不同请求中得到的文本元素响应。

如何更改每种情况下的响应?

1. 回应

{
  "declaration": {
    "attributes": {
      "version": "1.0",
      "encoding": "utf-8"
    }
  },
  "elements": [
    {
      "type": "element",
      "name": "Envelope",
      "attributes": {
        "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
      },
      "elements": [
        {
          "type": "element",
          "name": "Body",
          "elements": [
            {
              "type": "element",
              "name": "FetchCustResponse",
              "attributes": {
                "xmlns": "http://DC_API/vproxy/"
              },
              "elements": [
                {
                  "type": "element",
                  "name": "FetchCustResult",
                  "elements": [
                    {
                      "type": "text",
                      "text": "00_AccountNo: 242734005790, AccountType: 1, Address: Cell:340397882, Balance: 0.00, ContactNo: Cell:01039788200, MeterNo: 11178005790, MinAmount: 11,715.40, Name: David James"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

文本元素值的所需输出

"text" : {
"AccountNo": "04278005790",
"AccountType": "1",
"Address": "08039788217",
"Balance": "0.00",
"ContactNo": "08039788217",
"MeterNo": "04278005790",
"MinAmount": "11,715.40",
"Name": "David James"
}

2.

回复

{
  "declaration": {
    "attributes": {
      "version": "1.0",
      "encoding": "utf-8"
    }
  },
  "elements": [
    {
      "type": "element",
      "name": "Envelope",
      "attributes": {
        "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
      },
      "elements": [
        {
          "type": "element",
          "name": "Body",
          "elements": [
            {
              "type": "element",
              "name": "FetchUsageResponse",
              "attributes": {
                "xmlns": "http://DC_API/vproxy/"
              },
              "elements": [
                {
                  "type": "element",
                  "name": "FetchUsageResult",
                  "elements": [
                    {
                      "type": "text",
                      "text": "00_<NewDataSet>\r\n  <Table>\r\n    <ID>9872686</ID>\r\n    <AccountNo>04278005790</AccountNo>\r\n    <MeterNo>90278005790</MeterNo>\r\n    <AccounType>1</AccounType>\r\n    <PINs>5539</PINs>\r\n    <TotalAmount>4900.00</TotalAmount>\r\n    <CreditedAmount>4802.00</CreditedAmount>\r\n    <FleetComm>9.80</FleetComm>\r\n    <DealerComm>73.50</DealerComm>\r\n    <BankComm>14.70</BankComm>\r\n    <Date>2021-04-24T16:32:36.4+01:00</Date>\r\n    <IDWebTrans>13385836</IDWebTrans>\r\n    <NoOfCards>1</NoOfCards>\r\n    <BUID>12</BUID>\r\n    <TxnReference>BD069419</TxnReference>\r\n    <Token>24559787672457451531</Token>\r\n    <TokeUnit>195.70</TokeUnit>\r\n    <TokenAmt>4558.14</TokenAmt>\r\n    <Charges>341.86</Charges>\r\n  </Table>\r\n  <Table>\r\n    <ID>9763409</ID>\r\n    <AccountNo>04278005790</AccountNo>\r\n    <MeterNo>90278005790</MeterNo>\r\n    <AccounType>1</AccounType>\r\n    <PINs>5539</PINs>\r\n    <TotalAmount>5000.00</TotalAmount>\r\n    <CreditedAmount>4900.00</CreditedAmount>\r\n    <FleetComm>10.00</FleetComm>\r\n    <DealerComm>75.00</DealerComm>\r\n    <BankComm>15.00</BankComm>\r\n    <Date>2021-04-06T12:35:17.367+01:00</Date>\r\n    <IDWebTrans>13270588</IDWebTrans>\r\n    <NoOfCards>1</NoOfCards>\r\n    <BUID>12</BUID>\r\n    <TxnReference>108969129</TxnReference>\r\n    <Token>17218654018179855270</Token>\r\n    <TokeUnit>118.40</TokeUnit>\r\n    <TokenAmt>2520.93</TokenAmt>\r\n    <Charges>2479.07</Charges>\r\n  </Table>\r\n</NewDataSet>"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

文本元素值的所需输出

对象数组

请问有什么指针、解决方案或提示吗?

4

1 回答 1

0

对于第一个问题,我写了一个函数,到目前为止它正在工作。

 function filterDown(obj) {
      const obj1 = obj.elements;
      const obj2 = obj1[0];
      const obj3 = obj2.elements;
      const obj4 = obj3[0];
      const obj5 = obj4.elements;
      const obj6 = obj5[0];
      const obj7 = obj6.elements;
      const obj8 = obj7[0];
      const obj9 = obj8.elements;
      const obj10 = obj9[0];
      const obj11 = obj10.text;
      const obj13 = obj11.split("_");
      const obj14 = obj13[1].replace("Cell:", "");
      const obj15 = obj14.replace("Cell:", "");
      const obj16 = obj15.split(":");
      return {
        AccountNo: obj16[1].replace(", AccountType", "").trim(),
        AccountType: obj16[2].replace(", Address", "").trim(),
        Address: obj16[3].replace(", Balance", "").trim(),
        Balance: obj16[4].replace(", ContactNo", "").trim(),
        ContactNo: obj16[5].replace(", MeterNo", "").trim(),
        MeterNo: obj16[6].replace(", MinAmount", "").trim(),
        MinAmount: obj16[7].replace(", Name", "").trim(),
        Name: obj16[8].trim(),
      };
    }
    var desiredResult = filterDown(result);

于 2021-05-21T05:39:20.307 回答