【发布时间】:2021-11-25 18:15:51
【问题描述】:
我在 Powershell 中有一个 PSCustomObject,我想将其转换为 JSON。 Key-Value-Pair 应该在“children”数组下。你能帮忙吗?
Powershell:
#PowershellObject
$BookmarkContainer = [PSCustomObject]@{
"roots" = @{
"other" = @{
"children" = @()
}
"bookmark_bar" = @{
"children" = @()
"name" ="FavouriteBar"
"type" ="folder"
}
}
"version"=1
}
JSON 输出:
{
"roots": {
"bookmark_bar": {
"name": "FavouriteBar",
"type": "folder",
"children": [ ]
},
"other": {
"children": [ ]
}
},
"version": 1
}
预期输出:
{
"roots": {
"bookmark_bar": {
"children": [ ],
"name": "FavouriteBar",
"type": "folder"
},
"other": {
"children": [ ]
}
},
"version": 1
}
【问题讨论】:
-
哈希表默认不排序。使用
[ordered]属性来使用 OrderedDictionary。 -
@AdminOfThings 所说的。或者将内部项目转换为
[pscustomobject](也将保留语法顺序)
标签: json powershell convertto-json