powershell - Invoke-RestMethod 未正确传递有效负载中的 System.Object[] 字段
问题描述
json 有效负载如下所示:
{
"clients": [
{
"scope": "scope1",
"claim": "scope1",
"id": ["123", "567"]
},
{
"scope": "scope2",
"claim": "claim2",
"id": ["321", "765"]
}
]
}
> $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[]}}
能够通过访问来查看id值$inputjson.clients[0].id
,但是使用 in 的这个有效负载id=System.Object[]
,$inputjson
API 无法识别它。我正在使用Invoke-RestMethod
将有效负载发布到 API。
知道如何解析id=System.Object[]
有效负载中的内容,以便 API 正确识别它吗?
顺便说一句,API 使用 cURL cmd 识别相同的输入 json(原样)。
解决方案
您可以像这样发布纯 JSON,而不是使用ConvertFrom-Json
cmdlet 将 JSON 转换为 .NET 对象并发布 .NET 对象:
$myJson = @"
{
"clients": [
{
"scope": "scope1",
"claim": "scope1",
"id": ["123", "567"]
},
{
"scope": "scope2",
"claim": "claim2",
"id": ["321", "765"]
}
]
}
"@
Invoke-RestMethod -Uri "" -Method Post -Body $myJson -ContentType "application/json"
如果您想处理从文件中读取的 JSON,在发布之前,您可以像以前一样转换它并将其转换回纯 JSON。根据您的 PowerShell 版本,-Depth
在从 JSON 转换为 JSON 时,您可能需要使用该参数。
推荐阅读
- android - 以编程方式将具有权重的视图添加到 Kotlin 中的 LinearLayout
- ios - 如何让 iOS 15 中的搜索栏默认出现?
- iframe - iframe 可以在主机页面上设置会话存储吗
- mysql - 使用 Python 2.7 将数据连接并插入到 mysql 数据库 5.5
- android - 如何在 Flutter 中使用 SQFLITE 从数据库加载应用程序开始的数据?
- logstash - 按源 IP 过滤 Filebeat 输入
- javascript - 使用 chrome 扩展更改地理位置?
- spring - 由于 java.lang.NoSuchMethodError 无法启动 Spring 应用程序:org.springframework.core.log.LogDelegateFactory.getHiddenLog(Ljava/lang/String;)
- css - 为什么 el-menu-item 设置“浮动:正确”不起作用?
- go - 如何使 VScode Go 在多模块存储库中工作