我试图在power
shell中操作json文件数据并将其写回文件.甚至在操作之前,当我刚刚从文件中读取时,将它转换为powershell中的Json对象并将其写回文件,某些字符将被某些代码替换.以下是我的代码:
$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
某些字符正在被其代码替换.例如.:
< is replaced by <
> is replaced by >.
' is replaced by \u0027
样本输入:
{
"$schema": "https://source.com/template.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"accountName": {
"type": "string",
"defaultValue": ""
},
"accountType": {
"type": "string",
"defaultValue": ""
},
},
"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": "
" }, "accountType": { "type": "string", "defaultValue": "
" }, }, "variables": { "location": "sampleLocation", "account": "[parameters(\u0027accountName\u0027)]", "type": "[parameters(\u0027accountType\u0027)]", } }
为什么会发生这种情况,我该怎么做才能让它不是替换字符并以同样的方式写回来?