【发布时间】:2020-09-02 00:30:07
【问题描述】:
我有一个名为 index.json 的 JSON 文件,如下所示:
{
"First": {
"href": "test/one two three.html",
"title": "title one"
},
"Second": {
"href": "test/test test/one two three four.html",
"title": "title two"
}
}
我想写一个powershell脚本来更新每个对象的href,用-替换空格。
JSON 文件应如下所示:
{
"First": {
"href": "test/one-two-three.html",
"title": "title one"
},
"Second": {
"href": "test/test-test/one-two-three-four.html",
"title": "title two"
}
}
我从这篇文章中得到了一些帮助: Iterating through a JSON file PowerShell
我已经编写了一个脚本来获取所有的 href 值,我不知道如何在原始 JSON 文件中更新相同的值。我的脚本如下所示:
function Get-ObjectMembers {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True)]
[PSCustomObject]$obj
)
$obj | Get-Member -MemberType NoteProperty | ForEach-Object {
$key = $_.Name
[PSCustomObject]@{Key = $key; Value = $obj."$key"}
}
}
$a = Get-Content 'index.json' -raw | ConvertFrom-Json | Get-ObjectMembers
foreach($i in $a){
$i.Value.href.Replace(" ","-")
}
【问题讨论】:
标签: json powershell