简体   繁体   English

Powershell Invoke-RestMethod 将 XML 响应转换为 JSON 文本

[英]Powershell Invoke-RestMethod convert XML response to JSON text

I am trying to use the Invoke-RestMethod command to grab the XML response from a SOAP endpoint and convert it to JSON text.我正在尝试使用 Invoke-RestMethod 命令从 SOAP 端点获取 XML 响应并将其转换为 JSON 文本。 From what I have read, the Invoke-RestMethod should be converting the response to a custom object for me so this should be really simple... This is what I am trying....根据我的阅读,Invoke-RestMethod 应该为我将响应转换为自定义 object,所以这应该非常简单......这就是我正在尝试的......

$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody
$JSONResponse = ConvertTo-Json $APIRequest.Envelope -Depth 9

But this is what i get as the resulting JSON text?但这是我得到的结果 JSON 文本?

[ [ [ [ [ [ [ [] ], [], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ], [], [ [] ], [ [] ], [ [] ], [ [] ] ] ], [ [] ], [] ] ] ] ]

Can anyone suggest a standard way to do this without trying to interpret the XML response manually as XML text?任何人都可以建议一种标准方法来执行此操作,而无需尝试将 XML 响应手动解释为 XML 文本吗?

If I update my request to create an output file too then I can see the proper XML response in the file.如果我也更新创建 output 文件的请求,那么我可以在文件中看到正确的 XML 响应。

$APIRequest = Invoke-RestMethod -Method Post -Uri $SOAPEndpointURL -Headers $requestHeader -Body $requestBody #-OutFile "C:\SOAPResponse.txt"

If I change my conversion to not have the "-Depth 9" then the result is now this which is confusing?如果我将我的转换更改为没有“-Depth 9”,那么结果现在是这样的,这令人困惑吗?

[ [ [ "System.Xml.XmlElement" ] ] ]

To provide more detail, this is what the XML looks like when I call the end point using PostMan.为了提供更多细节,这是当我使用 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>

PowerShell doesn't offer direct conversion of XML to JSON out of the box. PowerShell 不提供开箱即用的 XML 到 JSON 的直接转换。

You'll need to process the XML returned by the API and turn it into one of the types ConvertTo-Json can handle, like hashtable.您需要处理 API 返回的 XML 并将其转换为 ConvertTo-Json 可以处理的类型之一,例如哈希表。

See an example below:请参见下面的示例:

# fetch XML
$xmlData = Invoke-RestMethod https://www.nasa.gov/rss/dyn/breaking_news.rss

# extract fields of interest
$hashtables = $xmlData | %{ @{ Id = $_.Identifier; Text = $_.InnerText } }

# convert hashtables into JSON
$hashtables | ConvertTo-Json

Using the newtonsoft serializer.使用newtonsoft序列化程序。
(Depending on your system/PowerShell version newtonsoft might already been available, otherwise see: How do I use Json.NET to parse json in PowerShell? ) (根据您的系统/PowerShell 版本,newtonsoft 可能已经可用,否则请参阅: How do I use Json.NET to parse json in PowerShell?

$Xml = [Xml]@"
<?xml version="1.0" encoding="utf-8"?>
<Book>
  <projects>
    <project name="Book1" date="2009-01-20">
      <editions>
        <edition language="English">En.Book1.com</edition>
        <edition language="German">Ge.Book1.Com</edition>
        <edition language="French">Fr.Book1.com</edition>
        <edition language="Polish">Pl.Book1.com</edition>
      </editions>
    </project>
  </projects>
</Book>
"@

[Newtonsoft.Json.JsonConvert]::SerializeXmlNode($Xml, 1)
{
  "?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
  },
  "Book": {
    "projects": {
      "project": {
        "@name": "Book1",
        "@date": "2009-01-20",
        "editions": {
          "edition": [
            {
              "@language": "English",
              "#text": "En.Book1.com"
            },
            {
              "@language": "German",
              "#text": "Ge.Book1.Com"
            },
            {
              "@language": "French",
              "#text": "Fr.Book1.com"
            },
            {
              "@language": "Polish",
              "#text": "Pl.Book1.com"
            }
          ]
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM