在 Powershell 中用不同的值查找并替换 JSON 中某个值的所有实例
Find and Replace all instances of a value in JSON with different value in Powershell
我有一个大 JSON 值需要用其他值替换。基本上我正在尝试在 powershell 中查找和替换所有内容。我想删除我们看到 Rock to Rocky 的地方
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",
"postalCode": "11",
}
]
},
{
"$type": "Office2",
"name": "Hawai2",
"streets": [
{
"name": "Rock",
"postalCode": "11",
}
]
}
]
}
我正在将 JSON 更改为对象,但不确定如何找到并替换所有内容。我想删除我们看到 Rock to Rocky 的地方
代码
$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';
Set-Content 在文件中。
(Get-Content -path C:\Users\Username\Folder\File.json -Raw) -replace 'Rock' , 'Rocky' | Set-Content -path C:\Users\Username\Folder\File.json;
我有一个大 JSON 值需要用其他值替换。基本上我正在尝试在 powershell 中查找和替换所有内容。我想删除我们看到 Rock to Rocky 的地方
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",
"postalCode": "11",
}
]
},
{
"$type": "Office2",
"name": "Hawai2",
"streets": [
{
"name": "Rock",
"postalCode": "11",
}
]
}
]
}
我正在将 JSON 更改为对象,但不确定如何找到并替换所有内容。我想删除我们看到 Rock to Rocky 的地方
代码
$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';
Set-Content 在文件中。
(Get-Content -path C:\Users\Username\Folder\File.json -Raw) -replace 'Rock' , 'Rocky' | Set-Content -path C:\Users\Username\Folder\File.json;