Puz*_*zik 22 powershell json file
我已将以下JSON文件转换为powershell表示对象.
{
"computer": [
{
"children": [
{
"children": [ {
"children": [ {
"path": "T:\Dropbox\kvaki.html",
"name": "kvaki",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Njusha",
"type": "folder"
}, {
"path": "T:\Dropbox\Europa.html",
"name": "Europa",
"type": "url",
"url": "http://example.com"
}, {
"path": "T:\Dropbox\math.html",
"name": "math",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Money",
"type": "folder"
}
],
"full_path_on_file_sys": "T:\Dropbox\"
}
]
Run Code Online (Sandbox Code Playgroud)
}
在使用powershell表示进行一些计算之后,我想将其作为JSON保存到文件中.但命令$jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json"以这种方式保存它
{
"computer": [
{
"children": " ",
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
Run Code Online (Sandbox Code Playgroud)
问题:如何实现正确保存复杂的PowerShell JSON表示?
Puz*_*zik 53
ConvertTo-Json的-depth参数解决了这个问题.
$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Run Code Online (Sandbox Code Playgroud)
只需将其传递给Set-Content或Out-File:
Get-Process powershell |
ConvertTo-Json |
Set-Content json.txt
Run Code Online (Sandbox Code Playgroud)