need to get desired data from string using batch

前端 未结 2 570
终归单人心
终归单人心 2021-01-26 20:00

i need to extract only URL and app id in the given string and saved in variables

url:{ \"url\":\"ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=201

2条回答
  •  自闭症患者
    2021-01-26 20:15

    Here's another hybrid solution using JScript. (Still save it with a .bat extension.)

    @if (@CodeSection == @Batch) @then
    
    @echo off
    setlocal
    
    set "JSONfile=test.json"
    
    for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%JSONfile%"') do set "%%~I"
    
    setlocal enabledelayedexpansion
    echo URL: !url!
    echo App: !app!
    endlocal
    
    goto :EOF
    
    @end // end batch / begin JScript chimera
    
    var fso = WSH.CreateObject('scripting.filesystemobject'),
        JSONfile = fso.OpenTextFile(WSH.Arguments(0), 1);
    
    eval('obj = {' + JSONfile.ReadAll() + '}');
    JSONfile.Close();
    
    function walk(tree) {
        for (var i in tree) {
            if (typeof tree[i] === 'object') walk(tree[i]);
            else WSH.Echo(i + '=' + tree[i]);
        }
    }
    
    walk(obj);
    

    Output:

    URL: ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640
    App: 61

    Delayed expansion was used to prevent the & in the URL from being evaluated.

    See this big fat warning if you don't control the generation of the JSON.

提交回复
热议问题