解释是(1,2),(3,4
) 是数组的数组,但是 Powershell 用管道分割了第一级|
,并且您没有为这些数组指定名称,因此序列化器提供它。首先尝试一下:
# First build your array of array
$z = (1,2),(3,4)
# convert it to JSON using the ,
,$z | ConvertTo-Json -Depth 5 -Compress
[psobject]@{"points"=$z} | ConvertTo-Json -Depth 5 -Compress
它给出了第一步:
{"value":[[1,2],[3,4]],"Count":2}
{"points":[[1,2],[3,4]]}
现在我提出的解决方案 :
# First build your array of array
$z = (1,2),(3,4)
# Then build a PSCustom object
$a = [pscustomobject]@{"series" = ,@{"metric"="custom.powershell.gauge"; "points"=$z}}
# At the end convert it to JSON
# don't forget the **Depth** parameter (use **Compress** to retreive one line like above)
$a | ConvertTo-Json -Depth 5
对我来说,它提供了接近您需要的东西:
{
"series": [
{
"points": [
[
1,
2
],
[
3,
4
]
],
"metric": "custom.powershell.gauge"
}
]
}