PowerShell:包含特殊字符的ConvertTo-Json问题 [英] PowerShell: ConvertTo-Json problem containing special characters

查看:633
本文介绍了PowerShell:包含特殊字符的ConvertTo-Json问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

PS>"Password& 123" |转换成杰森 输出为:密码\ 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"

-这是简化的JSON FILE的示例

{
    "Server1":
    {
        "username":"root",
        "password":"Password&dfdf"
    },
    "Server2":
    {
        "username":"admin",
        "password":"Password&1234"
    }
}

解决方案

这是由Convertto-Json的自动字符转义功能引起的,它会影响多个符号,例如<>\'&

ConvertFrom-Json将正确读取转义的字符.以您的示例为例:

PS C:\> {"Password\u0026123"} | ConvertFrom-Json
Password&123

您的示例代码导致文件中的字符转义,但是ConvertFrom-Json可以将其读回原始密码.见下文:

PS C:\> (Get-Content .\example.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

PS C:\> (Get-Content .\new.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

如果您需要将密码以不转义的方式存储,则可能需要一些更出色的工作.参见有关将Unicode字符串转换为转义的ascii字符串

或者,如果可能,请避免使用受影响的字符.

I am writing a script to make changes to a JSON file but when the file is converted back to JSON it expands special characters.

For example the JSON File contain passwords with "&". A quick way to replicate the problem is using following command:

PS> "Password&123" | Convertto-Json output is:"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"

--here is an example of simplified JSON FILE

{
    "Server1":
    {
        "username":"root",
        "password":"Password&dfdf"
    },
    "Server2":
    {
        "username":"admin",
        "password":"Password&1234"
    }
}

解决方案

This is caused by the automatic character escape feature of Convertto-Json and it affects several symbols such as <>\'&

ConvertFrom-Json will read the escaped characters properly. Using your example:

PS C:\> {"Password\u0026123"} | ConvertFrom-Json
Password&123

And your example code results in a file that has escaped characters, but ConvertFrom-Json can read it back to the original passwords. See below:

PS C:\> (Get-Content .\example.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

PS C:\> (Get-Content .\new.json -Encoding Ascii) -join "`n" | ConvertFrom-Json
Server1                                  Server2
-------                                  -------
@{username=root; password=Password&dfdf} @{username=admin; password=Password&1234}

If you need the passwords to be stored unescaped, some fancier work may be needed. See this thread about Converting Unicode strings to escaped ascii strings

Alternatively, avoid affected characters if possible.

这篇关于PowerShell:包含特殊字符的ConvertTo-Json问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆