我正在使用PowerShell向REST API发送一个POST请求。请求的消息体如下:
{
"title": "game result",
"attachments": [{
"image_url": "http://contoso/",
"title": "good work!"
},
{
"fields": [{
"title": "score",
"value": "100"
},
{
"title": "bonus",
"value": "50"
}
]
}
]
}
现在,以下 PowerShell 脚本产生错误的输出:
$fields = @(@{title='score'; value='100'},@{title='bonus'; value='10'})
$fieldsWrap = @{fields=$fields}
#$fieldsWrap | ConvertTo-Json
$attachments = @(@{title='good work!';image_url='http://contoso'},$fieldsWrap)
$body = @{title='game results';attachments=$attachments}
$json = $body | ConvertTo-Json
$json
若取消注释,第3行会输出正确的结果,然而第7行会产生:
Line 3 (if uncommented) produces the correct output, however line 7 produces:
{
"attachments": [{
"image_url": "http://contoso",
"title": "good work!"
},
{
"fields": "System.Collections.Hashtable System.Collections.Hashtable"
}
],
"title": "game result"
}
显然,它写出了HashTable
的类型名称,这是默认的ToString()
实现,我猜想如此。
我该如何获得正确的输出?