【发布时间】:2021-08-16 06:35:26
【问题描述】:
我正在尝试使用 Invoke-RestMethod 命令从 SOAP 端点获取 XML 响应并将其转换为 JSON 文本。根据我的阅读,Invoke-RestMethod 应该为我将响应转换为自定义对象,所以这应该非常简单...... 这就是我正在尝试的......
$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody
$JSONResponse = ConvertTo-Json $APIRequest.Envelope -Depth 9
但这就是我得到的结果 JSON 文本?
[ [ [ [ [ [ [ [] ], [], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ] ] ], [ [] ], [] ] ] ] ]
谁能建议一种标准方法来执行此操作,而无需尝试将 XML 响应手动解释为 XML 文本?
如果我也更新了创建输出文件的请求,那么我可以在文件中看到正确的 XML 响应。
$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody #-OutFile "C:\SOAPResponse.txt"
如果我将我的转换更改为没有“-Depth 9”,那么结果现在是这样的,这是令人困惑的吗?
[ [ [ "System.Xml.XmlElement" ] ] ]
为了提供更多细节,这是我使用 PostMan 调用端点时 XML 的样子。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetCustomerListResponse>
<GetCustomerListResult>
<Data>
<Customer>
<AddressLine1>123 Yellow Street</AddressLine1>
<AddressLine2/>
<AddressPostCode>1234</AddressPostCode>
<AddressState/>
<AddressSuburb>Sydney</AddressSuburb>
<Email>test@test.com</Email>
<PgnRowNo>1</PgnRowNo>
</Customer>
</Data>
<Error>0</Error>
<ErrorMessage i:nil="true"/>
</GetCustomerListResult>
</GetCustomerListResponse>
</s:Body>
</s:Envelope>
【问题讨论】:
-
您在转换为 Json 之前尝试过
[xml]$APIRequest = .....吗? -
“任何人都可以提出一种标准方法来执行此操作,而无需尝试将 XML 响应手动解释为 XML 文本吗?” - 没有神奇的“按我的意思”转换.显示 SOAP 响应和从中派生的 JSON。
-
this 有帮助吗?
标签: json xml powershell