首页 > 解决方案 > 如何替换 JSON 键值?

问题描述

{"enable"=false,
"datatype"="DB",
"report"=false}

我需要将enable值更改为true. 现在我正在使用以下代码:

(Get-Content 'C:\Shibi\Basic.json') -replace 'false', 'true' | Set-Content 'C:\Shibi\Basic.json'

标签: jsonpowershell

解决方案


如果您要使用文本文件而不是真正的 JSON 结构,则可以使用*-Content带有操作符的命令-replace

(Get-Content 'C:\Shibi\Basic.json') -replace '"enable"=false','"enable"=true' | Set-Content

注意: Set-Content将使用替换的数据更新 Basic.json 文件。

您可以将-Raw开关添加到Get-Content以保留原始文件的空格或换行符。


推荐阅读