首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用批处理脚本回显json文件中字段的值。

使用批处理脚本回显json文件中字段的值。
EN

Stack Overflow用户
提问于 2022-09-08 07:42:49
回答 2查看 40关注 0票数 0
代码语言:javascript
运行
复制
{
    "fields" : {
        "field_10061" : 24,   
        "field_10101" : "GB"
    }
}

我有一个名为.json的storage.json文件位于C:\files\storage.json中,storage.json的内容如下所示:-

我想要创建一个批处理文件,将"field_10101“的值存储在变量中,并回显它。我试过下面的命令,但它不起作用。

代码语言:javascript
运行
复制
set LOC=C:\files\storage.json

for /f  delims^=^"^ tokens^=3 %%A in ('findstr /R "field_10101" %LOC%') do set new=%%A

echo %new%.
EN

回答 2

Stack Overflow用户

发布于 2022-09-09 11:05:44

您正在处理JSON,所以请使用正确的JSON解析器!

西德尔

代码语言:javascript
运行
复制
FOR /F "delims=" %%A IN ('
  xidel -s "C:\files\storage.json" -e "$json/fields/field_10101"
') DO SET "new=%%A"

或者使用--output-format=cmd和点表示法,而不是XPath表示法:

代码语言:javascript
运行
复制
FOR /F "delims=" %%A IN ('
  xidel -s "C:\files\storage.json" -e "new:=($json).fields.field_10101" --output-format^=cmd
') DO %%A

或使用jq

代码语言:javascript
运行
复制
FOR /F "delims=" %%A IN ('
  jq -r ".fields.field_10101" "C:\files\storage.json"
') DO SET "new=%%A"
票数 1
EN

Stack Overflow用户

发布于 2022-09-08 21:40:03

代码语言:javascript
运行
复制
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文件的通用方法)

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73645375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档