问题描述
情况:使用batchscript从JSON中检索某些值。
我有以下批处理文本:
Situation: Using batchscript to retrieve certain values from a JSON.
I've got the following batchscript:
@ECHO off
ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('curl.exe -s http://e.omroep.nl/metadata/aflevering/%%A ^| jq.exe -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .start') DO SET ss=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .eind') DO SET to=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| jq.exe -r .tijdsduur') DO SET t=%%C
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
ENDLOCAL
PAUSE
它是做什么的?
进入npo.nl程序网址后,第一个for-loop将URL链接到prid: POMS_VPRO_850040
。在第二个for循环 curl.exe 中检索JSON ...:
What does it do?
Upon having entered a npo.nl program-url, the first for-loop strips the url down to the prid:POMS_VPRO_850040
. In the second for-loop curl.exe retrieves the JSON...:
parseMetadata({"STATUS":"OK","VERSION":"1.11.12","prid":"VPWON_1232766","titel":"Schuim & As","aflevering_titel":"","info":"Schuim & As met Jelle Brandt Corstius","ratio":"16:9","medium":"tv","gidsdatum":"2015-05-03","tijdsduur":"00:05:27","start":"00:23:13","eind":"00:28:40","url":"","webcast":1,"images":[{"size":"640x480","ratio":"4:3","url":"http:\/\/images.poms.omroep.nl\/image\/sx480\/c640x480\/606030.jpg"},{"size":"720x405","ratio":"16:9","url":"http:\/\/images.poms.omroep.nl\/image\/sx405\/c720x405\/606030.jpg"}],"omroepen":[{"naam":"VPRO"}],"pubopties":["adaptive","h264_bb","h264_sb","h264_std"],"tt888":"ja","serie":{"srid":"VPWON_1232748","serie_titel":"Buitenhof","serie_url":null},"sitestat":{"baseurl":"http:\/\/b.scorecardresearch.com\/p?c1=2&c2=17827132&ns_site=po-totaal","programurl":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","programurlpost":"category=uitzendinggemist&thema=informatief&po_source=video","baseurl_subtitle":"http:\/\/nl.sitestat.com\/klo\/po\/s","subtitleurl":"uitzendinggemist.publiekeomroep.ondemand.tv.player.tt888.buitenhof","subtitleurlpost":"category=uitzendinggemist&po_source=video&po_sitetype=webonly"},"reclame":"http:\/\/pubads.g.doubleclick.net\/gampad\/ads?_cookie_&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1&sz=_sz_&correlator=_correlator_&iu=\/9233\/_site_\/buitenhof&url=_url_&cust_params=genre%3Dinformatief%2Cnieuws%2Factualiteiten%26dur%3D3284%26prid%3DVPWON_1232766%26srid%3DVPWON_1232748%26player%3D_player_","streamSense":{"episode":"buitenhof","program":"buitenhof","station":"nederland_1","sitestatname":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","sko":"TRUE","sko_dt":"20150503","sko_pr":"buitenhof","sko_stid":"1","sko_ty":"tv.seg","sko_prid":"vpwon1232766","sko_t":"1210","sko_cl":"3284"}})
//epc
...并通过管道将其发送到 jq.exe ,它将删除非JSON数据 parseMetadata(
和 )// epc
并保留单行。这是由于两个原因:1)存在非JSON数据,我们无法处理任何内容,2)for-loop一次只处理1行。
随后的jq.exe的检索值对于没有双引号的指定对象。
只要curl.exe和jq.exe与batchscript位于相同的目录中,或者在%path%-variable中,这一切都正常工作:
...and sends it through a pipe to jq.exe which removes the non-JSON-data parseMetadata(
and ) //epc
and leaves the single line intact. This is for 2 reasons: 1) with non-JSON-data present we can't process anything, and 2) for-loops process only 1 line at a time.
The subsequent jq.exe's retrieve values for the specified objects without double quotes.
As long as curl.exe and jq.exe are in the same directory as the batchscript, or in the %path%-variable, this is working all fine:
Start: 00:23:13
End: 00:28:40
Duration: 00:05:27
现在我想从另一个地图调用curl.exe和jq.exe。一个在其中的空格:
Now I want to call curl.exe and jq.exe from another map. One with spaces in it:
SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"
@ECHO off
ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A ^| %jq% -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .start') DO SET ss=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .eind') DO SET to=%%C
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -r .tijdsduur') DO SET t=%%C
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
ENDLOCAL
PAUSE
对于第二个for循环,这会导致问题:
For the 2nd for-loop this causes problems:
'C:\map' is not recognized as an internal or external command,
operable program or batch file.
虽然'ECHO %% X ^ | %jq%'
确实工作,似乎'%curl%^ | %jq%'
不。所以出于某种原因,一旦管道中的两个变量被解析就出错了。
While 'ECHO %%X ^| %jq%'
does work, it seems '%curl% ^| %jq%'
doesn't. So for some reason things go wrong as soon as 2 variables in a pipe are parsed.
那么,再没有管道:
SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"
@ECHO off
ECHO Enter npo.nl program-url :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 for example
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -R -r -s ".[1+index(\"^(\"): rindex(\"^)\")]"') DO (
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .start') DO SET ss=%%D
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .eind') DO SET to=%%D
FOR /F "delims=" %%D IN ('ECHO %%C ^| %jq% -r .tijdsduur') DO SET t=%%D
ECHO Start: !ss!
ECHO End: !to!
ECHO Duration: !t!
)
)
)
ENDLOCAL
PAUSE
现在curl.exe和jq.exe都在for循环中。起初这似乎工作正常。 3个值被回显,但是出现问题:
Now curl.exe and jq.exe each in a for-loop. At first this seems to work fine. The 3 values are echoed, but then things go wrong:
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
Start: 00:23:13
End: 00:28:40
Duration: 00:05:27
像我之前说过的; for循环解析,一次只处理1行。第二行上的非JSON数据 // epc
导致for循环重新开始,这可能是非常错误的,你可以看到。这就是上面代码中curl和jq之间管道的原因。输出1个单行进行处理。可悲的是,这样做并不奏效。
Like I said before; for-loops parse and process only 1 line at a time. The non-JSON-data //epc
on the 2nd line causes the for-loop to start over, which goes horribly wrong as you can see. That's the reason for the pipe between curl and jq in the code above. To output 1 single line to process. Sadly that didn't work either...sigh.
当然,使用临时文件是curl和jq仍然在一个带空格的地图中的最后手段在其中,但我更喜欢使用变量,所以我试图解决管道问题。我已经在for循环中尝试了'usebackq',而在命令周围使用反引号,而不是单引号,但无效。
到目前为止,我还没有找到解决方案。有没有人有解释这个行为和如何解决?
Of course using temporary files is a last resort when curl and jq are still in a map with spaces in it, but I prefer to use variables, so I'm trying to solve the pipe-issue. I've tried 'usebackq' in the for-loop using backticks around the command instead of single-quotes for instance, but to no avail.
So far I haven't found a solution. Does anyone have an explanation for this behaviour and how to solve it?
推荐答案
感谢)
- For the cmd-shell you have to escape the double quotes with a line-feed \
.
- While 2 of the closing parenthesis are part of jq's syntax, the 1 between double quotes is not. So when in a for-loop, to prevent this for-loop from closing, you'd have to escape this one with a ^
.
- When jq's executable path is put in a variable with double quotes, to circumvent a WinXP bug, you'd then also have to put the entire command between ^"
, because the parenthesis are now considered special characters! This workaround is compatible with Vista and beyond. (See also DosTips.com)
这篇关于管道循环打破了双引号变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!