powershell - Powershell - convertTo-JSON 时添加“数据”块
问题描述
使用时如何添加"data" : [
块$var | convertTo-Json
?
的实际输出$var
只是一个具有一些属性的对象,然后使用命令将其转换为 json convertTo-Json
。
这是我得到的:
[
{
"controllerID": "0",
"DiskID": "0:1:0",
"cName": "PERCS130",
},
{
"controllerID": "0",
"DiskID": "0:1:1",
"cName": "PERCS130",
}
]
但我需要这样的东西:
{
"data": [
{
"controllerID": "0",
"DiskID": "0:1:0",
"cName": "PERCS130",
},
{
"controllerID": "0",
"DiskID": "0:1:1",
"cName": "PERCS130",
}
]
}
解决方案
您显示的 JSON 由于逗号的 after 是无效的"cName": "PERCS130"
。这可能是因为你在这里剥离了一些东西。
您需要做的是创建一个新的 json,其中一个新元素data
包含当前 json 作为数组:
$json = @"
[
{
"controllerID": "0",
"DiskID": "0:1:0",
"cName": "PERCS130"
},
{
"controllerID": "0",
"DiskID": "0:1:1",
"cName": "PERCS130"
}
]
"@ | ConvertFrom-Json
$newjson = @{'data' = @($json)}
$newjson | ConvertTo-Json -Depth 3 # you may need to up the Depth value
结果:
{ “数据”: [ { “控制器ID”:“0”, "磁盘ID": "0:1:0", "cName": "PERCS130" }, { “控制器ID”:“0”, "磁盘ID": "0:1:1", "cName": "PERCS130" } ] }
PowerShell 不会产生“漂亮”的 json。如果您需要将其转换为适当间隔的 json,请参阅我的函数Format-Json
推荐阅读
- android - AdMob 激励视频广告上限
- excel - SAPUI5 的 Sheetjs;将表格数据导出到电子表格
- router - Snort 错误:spp_session.c(2567) 无法初始化协议会话内存池
- tensorflow - 使用新类重新训练模型
- javascript - 如何在 plotly.js 中为 3D 图表设置多个轴
- html - 当我将鼠标悬停在帮助图标上时,工具提示隐藏在标题选项卡后面
- javascript - 将输入值存储在具有离子角 5 的数组中
- javascript - js正则表达式'|' 当任何一方匹配时匹配
- google-apps-script - Google Doc to PDF export Bug 取决于用户
- powershell - 如果我关闭它正在运行的 PS 窗口,FileSystemWatcher 将停止工作