问题
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
回答1:
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.
回答2:
this works if there's an url.txt
file in same directory with content:
{"url":{ "url":"ad.ifwcash.com/www/delivery/afr.php?zoneid=127&cb=2015738640", "app":61}}
Eventually you'll need to change name/location of the file on the third line (save the file bellow with .bat
extension):
@echo off
setlocal enableDelayedExpansion
set "jsonFile=.\url.txt"
set counter=1
for /f %%# in ('echo %jsonFile%^|mshta.exe "%~f0"') do (
set "variable_!counter!=%%#"
set /a counter=counter+1
)
set variable_
echo #############################
echo ### more batch code here ###
echo #############################
exit /b
<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(obj.url.url + "\r\n" + obj.url.app + "\r\n");
window.close();
</script>
来源:https://stackoverflow.com/questions/31700283/need-to-get-desired-data-from-string-using-batch