{
"fields" : {
"field_10061" : 24,
"field_10101" : "GB"
}
}
我有一个名为.json的storage.json文件位于C:\files\storage.json中,storage.json的内容如下所示:-
我想要创建一个批处理文件,将"field_10101“的值存储在变量中,并回显它。我试过下面的命令,但它不起作用。
set LOC=C:\files\storage.json
for /f delims^=^"^ tokens^=3 %%A in ('findstr /R "field_10101" %LOC%') do set new=%%A
echo %new%.
发布于 2022-09-09 11:05:44
您正在处理JSON,所以请使用正确的JSON解析器!
用西德尔
FOR /F "delims=" %%A IN ('
xidel -s "C:\files\storage.json" -e "$json/fields/field_10101"
') DO SET "new=%%A"
或者使用--output-format=cmd
和点表示法,而不是XPath表示法:
FOR /F "delims=" %%A IN ('
xidel -s "C:\files\storage.json" -e "new:=($json).fields.field_10101" --output-format^=cmd
') DO %%A
或使用jq
FOR /F "delims=" %%A IN ('
jq -r ".fields.field_10101" "C:\files\storage.json"
') DO SET "new=%%A"
发布于 2022-09-08 21:40:03
set LOC=C:\files\storage.json
for /f delims^=^"^ tokens^=3 %%A in ('findstr /R "field_10101" %LOC%') do set new=%%A
call :getvalue %new%
echo %value%
goto exit
:getvalue
set "value=%~3"
goto exit
:exit
使用真正的json解析器?(作为处理任何json文件的通用方法)
vbs/wsh/jscript (native) //maybay dll files?
powershell (native) //maybay in -Command "" and ignore Get-ExecutionPolicy ?
base64-encoded "internal components" //echo BASE64STRING... >tofile... ... certutil (native)
https://stackoverflow.com/questions/73645375
复制相似问题