Powershell convertto-json 给出了错误的格式

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

这是我的功能

def draw_graph(graph,only_nodes=False):
    fig,ax = plt.subplots(figsize=(12,6))

    kwargs = dict(node_size=25,ax=ax)
    if only_nodes:
        func = nx.draw_networkx_nodes
    else:
        func = nx.draw_networkx
        kwargs.update(with_labels=False)

    func(data.node_coords,graph,**kwargs)

这是 tfs.json

的结果

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



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

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


yjgwlh 回答:Powershell convertto-json 给出了错误的格式

您从 ConvertTo-JSON 收到的输出非常符合预期。您正在向 cmdlet 传递一个字符串,因此它以 JSON 形式返回一个字符串。

试试这个:

$output.value | ForEach-Object {
    [pscustomobject]@{
        id = $_.id
        outcome = $_.results.outcome
        idtc = $_.testCaseReference.id
    }
} | ConvertTo-Json | Out-File -FilePath .\tfs.json
本文链接:https://www.f2er.com/226028.html

大家都在问