Как получить и установить в качестве переменной одно значение из JSON файла в Windows Пакет - PullRequest
1 голос
/ 25 января 2020

Итак , в основном у меня есть 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"}

Это Api NASA APOD. Я хочу получить это конкретное значение "hdurl" и установить его как переменную URL внутри пакета: "https://apod.nasa.gov/apod/image/2001/RubinsGalaxy_hst2000.jpg"

Затем я хочу, чтобы оно загрузило изображение с этого сайта.

Честно говоря, я понятия не имею, как я могу это сделать. Буду очень признателен за любую помощь!

Ответы [ 2 ]

1 голос
/ 25 января 2020

Вы можете использовать 2 раза для l oop:

1-ые разделители :} в for l oop и ; (по умолчанию) во втором for l oop 2nd:

  • В командной строке:
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"}
0 голосов
/ 20 апреля 2020

Пожалуйста, используйте инструмент, который понимает 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' в текущий каталог.

...