问题描述
我有一个较大的JSON,其值需要替换为其他值。基本上,我试图在Powershell中查找并替换所有内容。我想删除我们看到的从岩石到岩石的任何地方
JSON
{
"Customer": [
{
"id": "123"
}
],"Nationality": [
{
"name": "US","id": "456"
}
],"address": [
{
"$type": "Home","name": "Houston","streets": [
{
"name": "Union","postalCode": "10",}
]
},{
"$type": "Office","name": "Hawai","streets": [
{
"name": "Rock","postalCode": "11",{
"$type": "Office1","name": "Hawaii","streets": [
{
"name": "Rock 678",{
"$type": "Office2","name": "Hawai2",}
]
}
]
}
我正在将JSON更改为对象,但不确定如何找到和替换所有对象。我想删除我们看到的从岩石到岩石的任何地方
$FileContent = Get-Content -Path "Test.json" -Raw | ConvertFrom-Json
$FileContent.address.streets.name = 'Rocky' // I want to do for all the instances.
$FileContent | ConvertTo-Json -Depth 100 | Out-File "Test.json" -Force
解决方法
如何使用Powershell替换文件中的字符串值?
-
用“ Rocky”替换“ Rock”;
-
文件中的设置内容。
(Get-Content -path C:\Users\Username\Folder\File.json -Raw) -replace 'Rock','Rocky' | Set-Content -path C:\Users\Username\Folder\File.json;