gpt4 book ai didi

windows - 如何使用批处理文件清除 json 中的字段?

转载 作者:可可西里 更新时间:2023-11-01 10:32:19 26 4
gpt4 key购买 nike

我希望在我的 config.json 中运行脚本时将密码字段设置为空白,它包含以下数据

{
"data": {
"user": "user1",
"password": "Password"
},
"data2":{
"user": "user2",
"password": "Password2"
}
}

知道怎么做吗?

最佳答案

您可以使用 RegEx 尝试使用此混合代码 Batch+Vbscript:

@echo off
Mode 52,3 & color 0A
Title Clear a field in json using batch file
Set "Tmpvbs=%temp%\Tmpvbs.vbs"
Set "InputFile=config.json"
Set "OutPutFile=result.txt"
Call :ReplaceData "%InputFile%" "%OutPutFile%"
echo(
Echo All passwords fields in "%InputFile%" are cleared
TimeOut /T 3 /nobreak>nul & exit
::****************************************************
:ReplaceData <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Set myRegExp = New RegExp
echo myRegExp.Global = True
echo myRegExp.Pattern = "(""password"":)(\W.*)"
echo ResultString = myRegExp.Replace(Data, """password"": """""^)
echo WScript.echo ResultString
)>"%Tmpvbs%"
cscript //nologo "%Tmpvbs%" < "%~1" > "%~2"
Move /y "%~2" "%~1">nul
If Exist "%Tmpvbs%" Del "%Tmpvbs%"
exit /b
::****************************************************

因此,您可以获得这样的 config.json:

{
"data": {
"user": "user1",
"password": ""
},
"data2":{
"user": "user2",
"password": ""
}
}

关于windows - 如何使用批处理文件清除 json 中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49044817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com