powershell - Powershell中的嵌套json循环
问题描述
我正在尝试使用 Powershell 中的 JSON 模式在 datalake 中创建嵌套文件夹。
尽管它可以工作,但它会为所有路径创建所有嵌套文件夹。我添加了测试 json。
我的杰森=
{
"system": [
{
"filesystem": "ing",
"foldersToCreate": [
{
"path": "path1/",
"foldersToCreate": [
{
"path": "test1/"
},
{
"path": "test2/"
}
]
},
{
"path": "path2/",
"foldersToCreate": [
{
"path": "test2/"
}
]
}
]
}
]
}
前任。
path1
-test1
-test2
Path2
-test2
代码正在运行,但也在 path1 中创建 test2。我想他们是属性的问题。
function Read-JsonConfig([string]$path)
{
$file = Get-Item $path
$plaintext = $file | Get-Content
$blockComments = '/\*(.*?)\*/'
$psObject = $cleantext | ConvertFrom-Json
return $psObject
}
function create-gen2-folders($folderConfig, $parentPath = '/')
{
$foldersToCreate = $folderConfig.foldersToCreate | Sort-Object { $_.path }
foreach($folderToCreate in $foldersToCreate)
{
$path = build-path $parentPath $folderToCreate.path
if($foldersToCreate.foldersToCreate -ne $null)
{
create-gen2-folders -folderConfig $folderToCreate -parentPath $path
}
}
}
解决方案
推荐阅读
- haskell - 当输入值是浮点类型而不是 Haskell 中的整数类型时如何创建错误消息
- php - 通过 AJAX 发送的数据在 $_POST 中不可用
- linux - 使用 bash 在 Linux 中的文件夹中查找拥有文件的所有用户
- python - 当我访问该视图时,未处理我的其中一个视图中的代码
- reactjs - 如何在反应中将来自json的数据与“this.props”连接起来
- php - 如何修复注意:未初始化的字符串偏移量:0
- python - 选择网格的所有顶点以分别访问每个顶点
- flutter - Mojave 10.14.4 更新后 ios-sim showdevicetypes 返回空
- typescript - 如何在 Angular 7 中分配公共变量(始终未定义)
- python - 在字典上使用 map lambda 打印 3 个值