【问题标题】:Powershell Invoke-RestMethod convert XML response to JSON textPowershell Invoke-RestMethod 将 XML 响应转换为 JSON 文本
【发布时间】: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


【解决方案1】:

PowerShell 不提供开箱即用的 XML 到 JSON 的直接转换。

您需要处理 API 返回的 XML,并将其转换为 ConvertTo-Json 可以处理的类型之一,例如哈希表。

请看下面的例子:

# 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

【讨论】:

  • 据我所知,您的“$xmlData”实际上不是XML 对象,而是已经反序列化的PowerShell Object[]'. ($xmlData.GetType()`)
  • @iRon,该行为取决于返回的 XML 类型。在我的示例中,它是一个 RSS 提要,Invoke-RestMethod 为其提供了特殊处理,它构建了一个 System.Xml.XmlElement 对象数组
  • 我需要从各个端点提取数据,因此希望不必专门识别感兴趣的字段。只是从 XML 到 JSON 的完整转换...
【解决方案2】:

使用newtonsoft 序列化程序。
(根据您的系统/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"
            }
          ]
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2015-06-20
    • 2015-02-04
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多