将Json文件转换为Powershell并返回到JSON文件 [英] Json file to powershell and back to json file
问题描述
我正在尝试在Powershell中处理json文件数据并将其写回到文件中.甚至在进行操作之前,当我刚刚从文件中读取文件,将其转换为Powershell中的Json对象并将其写回到文件中时,某些字符已被某些代码替换.以下是我的代码:
I am trying to manipulate json file data in powershell and write it back to the file. Even before the manipulation, when I just read from the file, convert it to Json object in powershell and write it back to the file, some characters are being replaced by some codes. Following is my code:
$jsonFileData = Get-Content $jsonFileLocation
$jsonObject = $jsonFileData | ConvertFrom-Json
... (Modify jsonObject) # Commented out this code to write back the same object
$jsonFileDataToWrite = $jsonObject | ConvertTo-Json
$jsonFileDataToWrite | Out-File $jsonFileLocation
某些字符已被其代码替换.例如:
Some characters are being replaced by their codes. E.g.:
< is replaced by \u003c
> is replaced by \u003e.
' is replaced by \u0027
样本输入:
{
"$schema": "https://source.com/template.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"accountName": {
"type": "string",
"defaultValue": "<sampleAccountName>"
},
"accountType": {
"type": "string",
"defaultValue": "<sampleAccountType>"
},
},
"variables": {
"location": "sampleLocation",
"account": "[parameters('accountName')]",
"type": "[parameters('accountType')]",
}
}
输出:
{
"$schema": "https://source.com/template.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"accountName": {
"type": "string",
"defaultValue": "\u003csampleAccountName\u003e"
},
"accountType": {
"type": "string",
"defaultValue": "\u003csampleAccountType\u003e"
},
},
"variables": {
"location": "sampleLocation",
"account": "[parameters(\u0027accountName\u0027)]",
"type": "[parameters(\u0027accountType\u0027)]",
}
}
为什么会发生这种情况,我该怎么做才能不替换字符并以相同的方式写回字符?
Why is this happening and what can I do to make it not to replace the characters and write them back the same way?
推荐答案
由于ConvertTo-Json
在后台使用.NET JavaScriptSerializer
,因此问题已经或多或少地得到了回答
Since ConvertTo-Json
uses .NET JavaScriptSerializer
under the hood, the question is more or less already answered here.
这里有一些无耻的复制粘贴:
Here's some shameless copypaste:
字符被正确编码!使用有效的JSON 库以正确访问JSON数据-它是有效的 JSON 编码.
The characters are being encoded "properly"! Use a working JSON library to correctly access the JSON data - it is a valid JSON encoding.
转义这些字符可防止通过JSON注入HTML-并且 JSON XML友好.也就是说,即使直接发出JSON 到JavaScript中(因为JSON是的有效2子集,所以经常这样做 JavaScript),则不能用于提前终止元素 因为相关字符(例如<,>)是在JSON中编码的 本身.
Escaping these characters prevents HTML injection via JSON - and makes the JSON XML-friendly. That is, even if the JSON is emited directly into JavaScript (as is done fairly often as JSON is a valid2 subset of JavaScript), it cannot be used to terminate the element early because the relevant characters (e.g. <, >) are encoded within JSON itself.
如果您真的需要将字符代码改回未转义的字符,则最简单的方法可能是对每个字符代码进行正则表达式替换.示例:
If you really need to turn character codes back to unescaped characters, the easiest way is probably to do a regex replace for each character code. Example:
$dReplacements = @{
"\\u003c" = "<"
"\\u003e" = ">"
"\\u0027" = "'"
}
$sInFile = "infile.json"
$sOutFile = "outfile.json"
$sRawJson = Get-Content -Path $sInFile | Out-String
foreach ($oEnumerator in $dReplacements.GetEnumerator()) {
$sRawJson = $sRawJson -replace $oEnumerator.Key, $oEnumerator.Value
}
$sRawJson | Out-File -FilePath $sOutFile
这篇关于将Json文件转换为Powershell并返回到JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!