我想在我的config.json中运行脚本时将密码字段设置为空白,它包含以下数据
{
"data": {
"user": "user1",
"password": "Password"
},
"data2":{
"user": "user2",
"password": "Password2"
}
}
知道怎么做吗?
答案 0 :(得分:1)
您可以使用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": ""
}
}