【问题标题】:How to get and set as variable one value from JSON file in Windows Batch如何从 Windows Batch 中的 JSON 文件中获取变量值并将其设置为一个值
【发布时间】:2020-05-11 17:37:53
【问题描述】:

所以,基本上我有一个 JSON 文件

{"date":"2020-01-25","explanation":"In this Hubble Space Telescope image the bright, spiky stars lie in the foreground toward the heroic northern constellation Perseus and well within our own Milky Way galaxy. In sharp focus beyond is UGC 2885, a giant spiral galaxy about 232 million light-years distant. Some 800,000 light-years across compared to the Milky Way's diameter of 100,000 light-years or so, it has around 1 trillion stars. That's about 10 times as many stars as the Milky Way. Part of a current investigation to understand how galaxies can grow to such enormous sizes, UGC 2885 was also part of astronomer Vera Rubin's pioneering study of the rotation of spiral galaxies. Her work was the first to convincingly demonstrate the dominating presence of dark matter in our universe.","hdurl":"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg","media_type":"image","service_version":"v1","title":"Rubin's Galaxy","url":"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg"}

这是一个 NASA APOD Api。我想获取一个特定的“hdurl”值并将其设置为批处理中的 URL 变量:“https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg

然后我希望它从该站点下载图像。

老实说,我不知道我应该如何做到这一点。 任何帮助将不胜感激!

【问题讨论】:

    标签: json windows batch-file extract


    【解决方案1】:

    您可以使用 2 次 for 循环:

    for循环中的第一个分隔符:},第二个for中的;(默认) 循环第二次:

    • 在命令行中:
    for /f tokens^=4*delims^=^:^} %i in ('type file.json')do for /f tokens^=1delims^=^"^  %I in ('echo/%i%j')do echo/%I && set "_imageURL=%I"
    
    @echo off 
    
    for /f tokens^=4*delims^=^:^} %%i in ('type file.json
    ')do for /f tokens^=1delims^=^"^  %%I in ('echo/%%i%%j')do echo/%%I && set "_imageURL=%%I"
    

    • 输出:
    https//apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg
    

    {"date":"2020-01-25","explanation":"In this Hubble Space Telescope image the bright, spiky stars lie in the foreground toward the heroic northern constellation Perseus and well within our own Milky Way galaxy. In sharp focus beyond is UGC 2885, a giant spiral galaxy about 232 million light-years distant. Some 800,000 light-years across compared to the Milky Way's diameter of 100,000 light-years or so, it has around 1 trillion stars. That's about 10 times as many stars as the Milky Way. Part of a current investigation to understand how galaxies can grow to such enormous sizes, UGC 2885 was also part of astronomer Vera Rubin's pioneering study of the rotation of spiral galaxies. Her work was the first to convincingly demonstrate the dominating presence of dark matter in our universe.","hdurl":"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg","media_type":"image","service_version":"v1","title":"Rubin's Galaxy","url":"https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst1024.jpg"}
    

    【讨论】:

      【解决方案2】:

      请使用能够理解 JSON 的工具。如果你愿意,可以试试xidel

      xidel -s input.json -e "$json/hdurl"
      https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg
      

      这个简单的提取查询返回你想要的 url。

      要将这个 url 分配给一个变量,你可以用老式的方式来做......

      FOR /F "delims=" %A IN ('xidel -s input.json -e "$json/hdurl"') DO SET URL=%A
      

      ...或者让xidel为您做...

      FOR /F "delims=" %A IN ('xidel -s input.json -e "URL:=$json/hdurl" --output-format^=cmd') DO %A
      

      (在批处理脚本中使用此命令时,请务必使用%%A 而不是%A

      要下载 jpg 文件,您可以再次使用 xidel

      xidel -s input.json -f "$json/hdurl" --download="."
      

      这会将 'RubinsGalaxy_hst2000.jpg' 下载到当前目录。

      【讨论】:

        猜你喜欢
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 2018-09-08
        • 2016-10-08
        • 1970-01-01
        相关资源
        最近更新 更多