Powershell convertto-json 格式错误

Powershell convertto-json gives a bad format

你好,我正在尝试使用 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"}
]

您从 ConvertTo-JSON 收到的输出非常符合预期。您将一个字符串传递给 cmdlet,因此它返回一个字符串 JSON.

试试这个:

$output.value | ForEach-Object {
    [pscustomobject]@{
        id = $_.id
        outcome = $_.results.outcome
        idtc = $_.testCaseReference.id
    }
} | ConvertTo-Json | Out-File -FilePath .\tfs.json