批处理解析值中带有冒号的json文件
我有一个像这样的批处理解析值中带有冒号的json文件,json,batch-file,Json,Batch File,我有一个像这样的json对象,我正试图在Batch中解析它(没有powershell): 我需要将True(或False)提取为一个变量,将Valid(或其他可能的字符串值)提取为另一个变量,但由于field1中的冒号和逗号都是变量,所以我遇到了问题。到目前为止,我所拥有的依赖于一致数量的冒号,所以它会崩溃 我认为最好先找到字符串“field_I_-want”或“other_-field_-I_-want”,然后再得到后面的子字符串,直到下一个逗号或大括号。然而,我在做这件事时遇到了一些麻烦;有
json
对象,我正试图在Batch
中解析它(没有powershell
):
我需要将True(或False)提取为一个变量,将Valid(或其他可能的字符串值)提取为另一个变量,但由于field1中的冒号和逗号都是变量,所以我遇到了问题。到目前为止,我所拥有的依赖于一致数量的冒号,所以它会崩溃
我认为最好先找到字符串“field_I_-want”或“other_-field_-I_-want”,然后再得到后面的子字符串,直到下一个逗号或大括号。然而,我在做这件事时遇到了一些麻烦;有人能帮我吗?目前我无法为您提供接受json路径的脚本,您需要将json对象成员硬编码到脚本中:
@echo off
setlocal enableDelayedExpansion
set "jsonFile=.\content.json"
::echo %jsonFile%|mshta.exe "%~f0"|more
for /f "tokens=* delims=" %%# in ('echo %jsonFile%^|mshta.exe "%~f0"') do (
echo %%#
)
exit /b %errorlevel%
<HTA:Application
ShowInTaskbar = no
WindowsState=Minimize
SysMenu=No
ShowInTaskbar=No
Caption=No
Border=Thin >
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<script language="javascript" type="text/javascript">
window.visible=false;
window.resizeTo(1,1);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
var fso2= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
var json=fso2.ReadLine();
var fso3=new ActiveXObject("Scripting.FileSystemObject");
var file = fso3.OpenTextFile(json, 1);
var strText = file.ReadAll();
file.Close();
var obj = JSON.parse(strText);
//fso.Write(strText);
fso.Write(obj.parameters.field_i_want + "\r\n");
fso.Write(obj.parameters.another_field_i_want + "\r\n");
window.close();
</script>
@echo关闭
setlocal enableDelayedExpansion
设置“jsonFile=。\content.json”
::echo%jsonFile%| mshta.exe“%~f0”|更多
对于/f“tokens=*delims=“%%#in('echo%jsonFile%^ | mshta.exe“%~f0””)do(
回声%%#
)
退出/b%errorlevel%
window.visible=false;
窗口。resizeTo(1,1);
var fso=new-ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
var fso2=new-ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
var json=fso2.ReadLine();
var fso3=新的ActiveXObject(“Scripting.FileSystemObject”);
var file=fso3.OpenTextFile(json,1);
var strText=file.ReadAll();
file.Close();
var obj=JSON.parse(strText);
//fso.Write(strText);
fso.Write(obj.parameters.field_i_want+“\r\n”);
fso.Write(obj.parameters.other_field_i_want+“\r\n”);
window.close();
为此,您至少需要internet explorer 9。该脚本使用
mshta
,它基本上是一个可以访问文件系统的internet explorer。它使用它的内部json解析器。我想我可以想出一个不闪烁小mshta窗口的版本。嘿,谢谢你-我实际上编写了一个python脚本,使用内置json解析器,然后通过批处理文件调用它
@echo off
setlocal enableDelayedExpansion
set "jsonFile=.\content.json"
::echo %jsonFile%|mshta.exe "%~f0"|more
for /f "tokens=* delims=" %%# in ('echo %jsonFile%^|mshta.exe "%~f0"') do (
echo %%#
)
exit /b %errorlevel%
<HTA:Application
ShowInTaskbar = no
WindowsState=Minimize
SysMenu=No
ShowInTaskbar=No
Caption=No
Border=Thin >
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<script language="javascript" type="text/javascript">
window.visible=false;
window.resizeTo(1,1);
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
var fso2= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(0);
var json=fso2.ReadLine();
var fso3=new ActiveXObject("Scripting.FileSystemObject");
var file = fso3.OpenTextFile(json, 1);
var strText = file.ReadAll();
file.Close();
var obj = JSON.parse(strText);
//fso.Write(strText);
fso.Write(obj.parameters.field_i_want + "\r\n");
fso.Write(obj.parameters.another_field_i_want + "\r\n");
window.close();
</script>