如何解决从 System.Collections.Hashtable 转换为特定的 JSON 格式在 PowerShell 中不起作用
我的哈希表值如下:
我正在尝试以以下格式获取 JSON:
我的哈希表值如下:
Name Value
---- -----
modesite {10000,10001...}
sites {11800,11900...}
Tsitesata {12000,12001...}
direct {13002,13003...}
我正在尝试以以下格式获取 JSON:
{
modesite: {
Ids: [
10000,10001,10002,....,10030,10032
]
},sites: {
Ids: [
11800,11801,11809
]
},Tsitesata: {
Ids: [
12000,12001,12030
]
},direct: {
Ids: [
13002,13003,13015
]
}
}
解决方法
您的 minimal reproducible example 不完整,因为它不包含任何代码(特别是 $exampleObj
来自哪里?)。但是,您可以接受 toastifer's answer,也可以尝试以下操作:
$exampleObj = @{
modesite = @( 10000,10001);
sites = @( 11800,11900);
Tsitesata= @( 12000,12001);
direct = @( 13002,13003);
}
$desiredObj = foreach ($key in $exampleObj.Keys) {
@{ $key = @{ Ids = $exampleObj.$Key }}
}
$desiredObj | ConvertTo-Json -Depth 8
,
不要将数组直接存储在键下。使用键“Ids”将其存储在另一个哈希表中。
$exampleObj=@{
modesite=@{ids=@(1,2,3)}
sites=@{ids=@(4,5,6)}
}
您还可以使用 ConvertFrom-Json 创建对象。插入您想要的输出,您将能够看到如何构建对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。