首页 > 解决方案 > PowerShell中的多个json对象到一个json对象获得额外的引号

问题描述

我试图使用 Powershell 在单个 JSON 数组中附加多个 Json 对象

从这个答案我得到了解决方案

$users = [System.Collections.ArrayList]::new();

 #calling some API in a loop Start

            $response = $json | ConvertTo-Json 
            $null = $data.Add($response);

 #calling some API in a loop End

    $result = @{ Data = $data};


    $result | ConvertTo-Json  | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Out-File 'C:\Jay\inv\$filename.json'

例如,假设来自 API 的 JSON 响应就像

{
    "host": "tet",
    "port": 443,
    "protocol": "http",
    "isPublic": false,
    "status": "READY",
    "startTime": 1585220081665,
    "testTime": 1585220127003,
    "engineVersion": "2.1.0",
    "criteriaVersion": "2009q",
    "endpoints": [
        {
            "delegation": 1
        }
    ]
}

然后根据脚本我期待的最终文件

{
    "Data" : [
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        },
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        }
    ]
}

但是我得到了像下面这样的额外引号,这会导致 json 中的错误

{
    "Data" : [
        "{
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        }",
        "{
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": [
                {
                    "delegation": 1
                }
            ]
        }"
    ]
}

有人可以帮助找出 powershell 脚本出了什么问题吗?

更新 1

$result = @{ Data = $data | ConvertFrom-Json };

我得到像

{
    "Data" : [
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": "     "
            ]
        },
        {
            "host": "tet",
            "port": 443,
            "protocol": "http",
            "isPublic": false,
            "status": "READY",
            "startTime": 1585220081665,
            "testTime": 1585220127003,
            "engineVersion": "2.1.0",
            "criteriaVersion": "2009q",
            "endpoints": "     "
            ]
        }
    ]
}

标签: jsonpowershell

解决方案


推荐阅读