json - 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": " "
]
}
]
}
解决方案
推荐阅读
- javascript - Chart.js 混合图表:线不从零开始
- python - 将文本文件读入字典时出现问题
- php - 如何修复 PHP AltoRouter URL 字符问题?
- android - 在 Flutter 中限制 Google Directions API 密钥
- javascript - 有没有办法动态插入/添加未定义为猫鼬集合中架构的一部分的新字段?
- c - 正则表达式意外模式匹配
- jquery - Jquery find(...).live 不是一个函数
- php - 致命错误:未捕获的错误:在 null 堆栈跟踪上调用成员函数 prepare():Database->query('SELECT jobs .*,...')Job->getAllJobs()
- r - 在 for 循环中使用“i”将函数应用于某些列
- dart - 是否可以初始化库?