批处理中解析简单的JSON字符串

时间:2016-04-02 14:25:39

标签: batch-file

如何在Batch中解析一个简单的JSON字符串?

例如,如果我有以下JSON字符串:

{   " ...":" ...&#34 ;,   "年":2016年,   "时间":" 05:01",   " ...":" ...&#34 ;, }

这个JSON字符串有许多元素,其中两个是"年"和"时间"。从这个字符串,我想提取"年"的值。和"时间"在没有使用任何外部库的两个变量中(即,我不想为此安装下载任何外部工具,新安装的Windows应该具备执行此操作的所有必要工具)。

5 个答案:

答案 0 :(得分:10)

这是一个混合的Batch + PowerShell解决方案。 PowerShell对JSON文本进行客观化(反序列化)并以key = value格式输出,以便通过批处理for /f循环捕获并设置为批处理变量。用.bat扩展名保存它并给它一个镜头。

<# : batch portion (contained within a PowerShell multi-line comment)
@echo off & setlocal

set "JSON={ "year": 2016, "time": "05:01" }"

rem # re-eval self with PowerShell and capture results
for /f "delims=" %%I in ('powershell "iex (${%~f0} | out-string)"') do set "%%~I"

rem # output captured results
set JSON[

rem # end main runtime
goto :EOF

: end batch / begin PowerShell hybrid code #>

add-type -AssemblyName System.Web.Extensions
$JSON = new-object Web.Script.Serialization.JavaScriptSerializer
$obj = $JSON.DeserializeObject($env:JSON)

# output object in key=value format to be captured by Batch "for /f" loop
foreach ($key in $obj.keys) { "JSON[{0}]={1}" -f $key, $obj[$key] }

然后,如果您只想要年份和时间值,只需使用%JSON[year]%%JSON[time]%

如果您正在从.json文件中读取JSON,则可以让PowerShell部分读取该文件,将($env:JSON)替换为((gc jsonfile.json))。那么你根本不会依赖于你的JSON是多线的还是美化的还是缩小的。无论如何,它都会被反序列化。

如果需要考虑执行速度,您可能更喜欢Batch + JScript混合解决方案。它将JSON反序列化为与PowerShell解决方案相同的对象,但从Batch上下文调用JScript比从Batch调用PowerShell命令或脚本要快。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "JSON={ "year": 2016, "time": "05:01" }"

rem // re-eval self with JScript interpreter and capture results
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0"') do set "%%~I"

rem // output captured results
set JSON[

rem // end main runtime
goto :EOF

@end // end Batch / begin JScript hybrid code

var htmlfile = WSH.CreateObject('htmlfile'),
    txt = WSH.CreateObject('Wscript.Shell').Environment('process').Item('JSON');

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
var obj = htmlfile.parentWindow.JSON.parse(txt);
htmlfile.close();

for (var i in obj) WSH.Echo('JSON[' + i + ']=' + obj[i]);

与第一个PowerShell混合解决方案一样,如果您愿意,可以通过从JScript部分读取.json文件来解析多行JSON(通过创建Scripting.FileSystemObject对象并调用它{ {1}}和.OpenTextFile()方法)。

这是另一个纯批处理解决方案,但是将key = value对设置为关联数组,以避免像Aacini解决方案那样踩踏.ReadAll()。我认为将JSON解析为辅助语言中的对象而不是纯批处理中的平面文本会更好,但我也意识到最好的答案并不总是最受欢迎。

%time%

答案 1 :(得分:8)

@echo off
setlocal

set string={ "other": 1234, "year": 2016, "value": "str", "time": "05:01" }

rem Remove quotes
set string=%string:"=%
rem Remove braces
set "string=%string:~2,-2%"
rem Change colon+space by equal-sign
set "string=%string:: ==%"
rem Separate parts at comma into individual assignments
set "%string:, =" & set "%"

echo other="%other%"
echo year="%year%"
echo value="%value%"
echo time="%time%"

输出:

other="1234"
year="2016"
value="str"
time="05:01"

这种方法的一个小不方便就是&#34;时间&#34;批量动态变量将被新的JSON替换,但是如果你使用setlocal命令,这一点不会引起任何问题。

答案 2 :(得分:5)

有不同的方式,这是一个。

@echo off
set string={ "year": 2016, "time": "05:01" }
set string=%string:"=%

for /f "tokens=3,5" %%a in ('echo %string%') do set d=%%a&set t=%%b
echo -%d%- -%t%-
pause & goto :EOF

这是第二个:

@echo off
set string={ "year": 2016, "time": "05:01" }

for /f "tokens=3,5" %%a in ('echo %string%') do set d=%%~a&set t=%%~b
echo -%d%- -%t%-
pause & goto :EOF

答案 3 :(得分:1)

jsonextractor.bat

for /f "tokens=* delims=" %%a in ('jsonextractor.bat simple.json year') do set "year=%%~a"

for /f "tokens=* delims=" %%a in ('jsonextractor.bat simple.json time') do set "time_=%%~a"

echo %year% -- %time_%

答案 4 :(得分:0)

使用https://stedolan.github.io/jq/,无需批量使用Powershell或字符串操作。

从json字符串获取字段值的示例:

set "year={ "other": 1234, "year": 2016, "value": "str", "time": "05:01" } | jq .year"

echo %year%

给你2016年