【问题标题】:ConvertFrom-Json not parsing array within arrayConvertFrom-Json 不解析数组内的数组
【发布时间】:2021-07-28 08:52:09
【问题描述】:

json 有效载荷如下所示:

{
  "clients": [
    {
      "scope": "scope1",
      "claim": "scope1",
      "id": ["123", "567"]
    },
    {
      "scope": "scope2",
      "claim": "claim2",
      "id": ["321", "765"]
    }
  ]
}

当尝试获取 pwsh 变量中的 json 时,我没有得到 id 值,它只显示 System.Object[]

> $inputjson = (((get-content .\inputfile.json) -Join " " ) | convertfrom-json )

> echo $inputjson

 clients                                                                               
 -------                                                                 
 {@{scope=scope1; claim=scope1; id=System.Object[]}, @{scope=scope2; claim=claim2; id=System.Object[]}}

任何关于如何获得这些数组值的帮助?

【问题讨论】:

  • 您仍然可以访问这些值,尝试$json.clients 而不使用echo。和 $json.clients[0].id 例如将返回 scope1 的 ID
  • @SantiagoSquarzon:谢谢。我可以访问它,但实际的问题是 API 不接受有效负载,因为“id”字段中有这个 System.Object[]。我正在使用Invoke-RestMethod -Method POST -Headers $headers -Body $inputjson -Uri $uri 调用 API

标签: c# powershell powershell-2.0 powershell-3.0 azure-powershell


【解决方案1】:

正如@SantiagoSquaron 在评论中所说,我们可以通过 $inputjson.clients[0].id 访问数组内容。谢谢@SantiagoSquaron。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多