需要使用批处理从字符串获取需要的数据 [英] need to get desired data from string using batch
问题描述
我需要只提取URL和应用程序ID给定的字符串并保存在变量
网址:{URL:ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640,应用程序:61}
块引用>最后的结果就像
variable_1:ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640variable_2的:61
解决方案下面是使用JScript的混合动力解决方案。 (还有一个.bat扩展名保存它。)
@if(@ codeSection == @Batch)@then关闭@echo
SETLOCAL集JSONfile = test.jsonFOR / Fdelims =%%我在('CSCRIPT / NOLOGO / E:JScript的%〜F0%JSONfile%)并设置%%〜我SETLOCAL enabledelayedexpansion
回音!网址:网址!
回声应用:应用程序!
ENDLOCALGOTO:EOF@end //结束批量/开始JScript的嵌合体变种FSO = WSH.CreateObject('Scripting.FileSystemObject的'),
JSONfile = fso.OpenTextFile(WSH.Arguments(0),1);的eval('OBJ = {'+ JSONfile.ReadAll()+'}');
JSONfile.Close();功能散步(树){
对于(树VAR我){
如果(typeof运算树[I] ===对象),步行(树[I]);
其他WSH.Echo(我+=+树[I]);
}
}走(OBJ);输出:
网址:ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640结果
应用程序:61
块引用>延迟扩展用于prevent的
&放大器;
从被评估的网址请参阅这块大肥肉警告的,如果你不控制JSON的产生。
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=2015738640", "app":61}
final result like
variable_1 : ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640 variable_2 : 61
解决方案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: 61Delayed 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.
这篇关于需要使用批处理从字符串获取需要的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!