我想操縱PowerShell中的JSON文件數據,並將其寫回到文件。甚至在操作之前,當我剛剛從文件中讀取時,將它轉換爲PowerShell中的Json對象並將其寫回到文件中,某些字符正被一些代碼所替代。以下是我的代碼: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 \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)]",
}
}
爲什麼會出現這種情況,我能做些什麼,使之不替換字符並寫回同樣的方式?