Batch file 如何使用批处理脚本提取json文件的url
我想要从json文件中提取url,并使用批处理脚本将其存储到变量中 下面是我的json文件Batch file 如何使用批处理脚本提取json文件的url,batch-file,Batch File,我想要从json文件中提取url,并使用批处理脚本将其存储到变量中 下面是我的json文件 { "url": "https://api.github.com/repos/octocat/Hello-World/releases/1", "html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0", "assets_url": "https://api.github.com/repos/octocat/Hel
{
"url": "https://api.github.com/repos/octocat/Hello-World/releases/1",
"html_url": "https://github.com/octocat/Hello-World/releases/v1.0.0",
"assets_url": "https://api.github.com/repos/octocat/Hello-World/releases/1/assets",
"upload_url": "https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
"tarball_url": "https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0",
"zipball_url": "https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0",
"id": 1,
"node_id": "MDc6UmVsZWFzZTE=",
"tag_name": "v1.0.0",
"target_commitish": "master",
}
我想把“upload\u url”
和{?name,label}“
之间的那一行存储到变量中
https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets
我可以获得批处理脚本命令来实现这一点吗
我尝试了以下命令:
for /f "tokens=2 delims=:" %%# in (' type tmpCurl.json^|find /i "upload_url"') do echo %%#
但它提供了这么多输出:
"https
这应该是您所需要的:
@echo off
for /f "tokens=1,* delims=:" %%a in ('type tmpCurl.json ^| findstr /i "upload_url"') do (
for /f "tokens=1,* delims={" %%i in (%%b) do set "result=%%i"
)
echo %result%
第一个循环将:
delimeter上的字符串拆分,并将:
之前的所有内容分配给元变量%%a
,将下面的所有内容分配给%%b
https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}",
第二个循环将{
上的字符串%%b
拆分,并将{
之前的所有内容分配给%%i
留下:
https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets
根据您提供的
.json
内容,这样的内容可能足以满足您的以下要求:
@For/F Tokens^=3Delims^={^“^%%A In('Find“upload_url”^由于最后第二行中的尾随逗号,您的示例Json无效。
最好使用能够处理Json的Json工具/脚本语言
批量包装的PowerShell单层衬里
:: Q:\Test\2019\05\18\SO_56197577.cmd
@Echo off
For /f "Usebackq delims=" %%U in (`powershell -NoP -C ^
"(Get-Content tmpCurl.json | ConvertFrom-Json).upload_url.split('{')[0]"
`) Do Set "upload_url=%%U"
set upload_url
样本输出
> Q:\Test\2019\05\18\SO_56197577.cmd
upload_url=https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets
现在输出为https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?名称,标签},“上传url”:https://uploads.github.com/repos/octocat/Hello-World/releases /1/资产
我想要https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets
你能帮我弄到吗?最新的代码正是我想要的。非常感谢。你也能把解释也放进去吗?解释补充了。