gpt4 book ai didi

json - 如何使用powershell按键对对象进行排序

转载 作者:行者123 更新时间:2023-12-05 04:06:59 24 4
gpt4 key购买 nike

我有以下 json 文件,我希望它按键/名称排序。但到目前为止,我一直无法弄清楚如何根据键/名称实际对 json 对象进行排序。

原始设置.json

{
"files.trimTrailingWhitespace": true,
"workbench.startupEditor": "newUntitledFile",
"editor.tabSize": 4,
"editor.formatOnSave": true,
"editor.detectIndentation": false,
"editor.trimAutoWhitespace": true
}

代码:

# Get Json File
$JsonFile = 'C:\Settings.json'
# Convert from Json File to Json Object
$Json = Get-Content $JsonFile | Out-String | ConvertFrom-Json
# Sort Json Object (Does Not Work!!!)
$Json = $Json | Sort-Object -Property Name
#Convert Json Object to Json File
$Json | ConvertTo-Json -depth 100 | Set-Content $JsonFile

新设置.Json

{
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"editor.tabSize": 4,
"editor.trimAutoWhitespace": true
"files.trimTrailingWhitespace": true,
"workbench.startupEditor": "newUntitledFile"
}

最佳答案

$json | Select-Object ($json | Get-Member -MemberType NoteProperty).Name | ConvertTo-Json

关于json - 如何使用powershell按键对对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49200592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com