你好,
我在生成 HTTP API 调用要用的 JSON,有一处语法想不明白。
代码如下。
$payload = [ordered]@{
resourceType = 'Parameters';
parameter = @{
name = 'Valueset'
}
}
write-host ($payload | ConvertTo-JSON)
先定义哈希表,再把它转换成 JSON,这个思路对吗?我是 Powershell 新手,不想在自学时养成坏习惯。
具体来说,我需要在下面两种情况下让 parameter 成为数组。
实际有多个 name 属性时。
还有像这里一样只有一个,但 API 要求这个值用 json 数组表示时。
我试了几种办法都没成功,希望有人指点一下正确写法。
谢谢
从英语翻译·查看原文Hi,
I'm building JSON to send in an HTTP API call, and one aspect of the syntax has me stuck.
Here's the code:
$payload = [ordered]@{
resourceType = 'Parameters';
parameter = @{
name = 'Valueset'
}
}
write-host ($payload | ConvertTo-JSON)
Am I right that defining a hash table and then turning that into JSON is the correct approach? As a Powershell newcomer, I want to avoid picking up bad habits while teaching myself.
What I specifically need is an array for parameter under two circumstances:
If there's actually more than one name attribute.
If there's just one, like here, but the API expects that value to be represented as a json array.
I've attempted a few approaches without success, so I'd welcome any guidence on getting this right.
Cheers