相关疑难解决方法(0)

PowerShell:ConvertTo-Json包含特殊字符的问题

我正在编写一个脚本来更改JSON文件,但是当文件转换回JSON时,它会扩展特殊字符.

例如,JSON文件包含带有"&"的密码.复制问题的快速方法是使用以下命令:

PS>"密码&123"| Convertto-Json输出为:"Password\u0026123"

##Here is how I import the JSON FILE:
$jsonfile = (Get-Content .\example.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
##Exporting JSON FILE without modifying it.
$jsonfile  | ConvertTo-Json |Out-File "new.json"
Run Code Online (Sandbox Code Playgroud)

- 这是一个简化的JSON文件的例子

{
    "Server1":
    {
        "username":"root",
        "password":"Password&dfdf"
    },
    "Server2":
    {
        "username":"admin",
        "password":"Password&1234"
    }
}
Run Code Online (Sandbox Code Playgroud)

powershell json

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

json ×1

powershell ×1