0

您好我正在尝试使用 convertto-json 从另一个输出创建一个 json 文件并保存它,但我得到了一个错误的 json 格式

这是我的功能


$output.value | ForEach-Object {
    "{"+"id"+':'+ $_.id+','+"outcome" +':'+ '"'+$_.results.outcome+'"'+','+"idtc"+':' +$_.testCaseReference.id  + "}"
} | ConvertTo-Json| Out-File -FilePath .\tfs.json



这是tfs.json的结果

[
    "{id:11431,outcome:\"passed\",idtc:70155}",
    "{id:11432,outcome:\"unspecified\",idtc:70156}",
    "{id:11433,outcome:\"failed\",idtc:70157}",
    "{id:11434,outcome:\"unspecified\",idtc:70158}"
]


任何人都可以帮助为什么它确实以这种方式出现而不是

[
    {"id":11431,"outcome":"passed","idtc":70155"},
    {"id":11432,"outcome":"unspecified","idtc":70156"},
    {"id":11433,"outcome":"failed","idtc":70157"},
    {"id":11434,"outcome":"unspecified","idtc":70158"}
]
4

1 回答 1

4

您收到的输出ConvertTo-JSON几乎是预期的。您正在将字符串传递给 cmdlet,因此它将字符串作为 JSON 返回。

试试这个:

$output.value | ForEach-Object {
    [pscustomobject]@{
        id = $_.id
        outcome = $_.results.outcome
        idtc = $_.testCaseReference.id
    }
} | ConvertTo-Json | Out-File -FilePath .\tfs.json
于 2021-06-19T23:38:24.130 回答