批处理文件Parse JSON

时间:2016-07-03 16:27:43

标签: json batch-file

这是我想要设置的JSON" 1.1.3"这是"稳定的版本"对象到批处理文件中的变量(没有引号)。 我希望它具有动态性,也许将来作曲家将其改为"版本":" 1.1.3.6"甚至"版本":" 1.1.3-beta2",我想得到任何版本的价值。

谢谢。

myFile.json

{
    "stable": [{"path": "/download/1.1.3/composer.phar", "version": "1.1.3", "min-php": 50300}],
    "preview": [{"path": "/download/1.1.3/composer.phar", "version": "1.1.3", "min-php": 50300}],
    "snapshot": [{"path": "/composer.phar", "version": "334d0cce6b056e7555daf4c68c48cbe40ee4d51a", "min-php": 50300}]
}

3 个答案:

答案 0 :(得分:4)

为了皮特的爱!使用JSON解析器。数据已经是分层的。将对象进行客体化并挖掘层次结构比将其标记化并计算线条/单词更为优雅。

@echo off & setlocal

set "jsonfile=test.json"

set "psCmd="add-type -As System.Web.Extensions;^
$JSON = new-object Web.Script.Serialization.JavaScriptSerializer;^
$JSON.DeserializeObject($input).stable.version""

for /f %%I in ('^<"%jsonfile%" powershell -noprofile %psCmd%') do set "version=%%I"

echo Version: %version%

另外,如果您正在调用PowerShell代码段,您还可以使用Invoke-WebRequest从网上获取JSON。

@echo off & setlocal

set "jsonURL=https://getcomposer.org/versions"

set "psCmd="add-type -As System.Web.Extensions;^
$JSON = new-object Web.Script.Serialization.JavaScriptSerializer;^
$JSON.DeserializeObject((Invoke-WebRequest %jsonURL%).content).stable.version""

for /f %%I in ('powershell -noprofile %psCmd%') do set "version=%%I"

echo Version: %version%

如果您需要与XP / Vista兼容,或者您​​只想要一个运行速度比PowerShell助手快的脚本,那么您可以使用JScript来达到同样的效果。

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

set "URL=https://getcomposer.org/versions"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%URL%"') do set "ver=%%I"

echo Version: %ver%

goto :EOF
@end // end Batch / begin JScript hybrid code

var htmlfile = WSH.CreateObject('htmlfile'),
    x = WSH.CreateObject("Microsoft.XMLHTTP");

x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState != 4) WSH.Sleep(50);

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

WSH.Echo(obj.stable[0].version);

答案 1 :(得分:0)

如果我们假设位置永远不会改变代码:

<li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Graderworks <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li>
                              <a href="https://garlandindustriesllc.com/index.php/pages/view/graderworks">Basic Information</a>
                        </li>
                        <li>
                            <a href="https://garlandindustriesllc.com/index.php/pages/view/examples">Examples</a>

                        </li>
                        <li>
                            <a href="https://garlandindustriesllc.com/index.php/pages/view/price">Price</a>

                        </li>

                        <li>
                            <a href="https://garlandindustriesllc.com/index.php/pages/view/download">Download</a>

                        </li>

                    </ul>
                </li>-

答案 2 :(得分:0)

Xidel非常简单:

FOR /F "delims=" %%A IN ('xidel -s myFile.json -e "ver:=$json/(stable)()/version" --output-format^=cmd') DO %%A