簡體   English   中英

如何從 JSON 中獲取價值?

[英]How can I get value from JSON?

我試圖從 JSON 中獲取“url”,我以這種方式從響應中獲得:

print('Please specifie the date in such way: YYYY-MM-DD.')
startdate = input()
enddate = input()
r = requests.get(f'https://api.nasa.gov/planetary/apod?api_key=Zex7CBAHQmbVfomUeIOyZXt9d8JccD4R50fNNhal&start_date={startdate}&end_date={enddate}')
parsed = r.json()
url = parsed['url']
print(url)

運行程序后,我收到此消息:

TypeError: list indices must be integers or slices, not str

我得到的 JSON 看起來像這樣:

[{'date': '2022-06-08', 'explanation': "What are those unusual streaks? Some images of planet Earth show clear bright streaks that follow the paths of ships.  Known as ship tracks, these low and narrow bands are caused by the ship's engine
exhaust. Water vapor condenses around small bits of exhaust known as aerosols, which soon grow into floating water drops that efficiently reflect sunlight. Ship tracks were first discovered in 1965 in Earth images taken by NASA's TIROS satellites.  Multiple ship tracks are visible across the featured image that was captured in 2009 over the Pacific Ocean by the MODIS instrument on NASA's Terra satellite. Inspired by ship-tracks, some scientists have suggested deploying a network of floating buoys in the worlds' oceans that spray salt-aerosol containing sea-water into the air so that, with the help of the wind, streams of sunlight-reflecting clouds would also form.  Why do this?  These human-made clouds could reflect so much sunlight they might help fight global warming.    Today is: World
Oceans Day", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/ShipTracks_Terra_4892.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Ship Tracks over the Pacific Ocean', 'url': 'https://apod.nasa.gov/apod/image/2206/ShipTracks_Terra_960.jpg'}, {'copyright': 'Wolfgang Zimmermann', 'date': '2022-06-09', 'explanation': "These cosmic clouds of gas and dust drift through rich star fields
along the plane of our Milky Way Galaxy toward the high flying constellation Cygnus. They're too faint to be seen with the unaided eye though, even on a clear,
dark night. Image data from a camera and telephoto lens using narrowband filters was used to construct this 10 degree wide field of view. The deep mosaic reveals a region that includes star forming dust clouds seen in silhouette against the characteristic glow of atomic hydrogen and oxygen gas. NGC 6888 is the standout emission nebula near the top. Blown by winds from a massive Wolf-Rayet star it's about 25 light-years across and known as the Crescent Nebula. A faint bluish curl just below center in the frame is also the signature of a Wolf-Rayet star. Burning fuel at a prodigious rate and near the end of their stellar lives, both stars will ultimately go out with a bang in a spectacular supernova explosion. Toward the right, a massive, young O type star powers the glow of Sh2-101, the Tulip Nebula.", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/CygWideHa-OIIIBiColorImage2_crop2_2048.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Cosmic Clouds in Cygnus', 'url': 'https://apod.nasa.gov/apod/image/2206/CygWideHa-OIIIBiColorImage2_crop2_1024.jpg'}, {'copyright': 'Nicolas Rolland', 'date': '2022-06-10', 'explanation': "This colorful telescopic field of view features a trio of interacting galaxies almost 90 million light-years away, toward the constellation Virgo. On the right two spiky, foreground Milky Way stars echo the
extragalactic hues, a reminder that stars in our own galaxy are like those in distant island universes. With sweeping spiral arms and obscuring dust lanes, the
dominant member of the trio, NGC 5566, is enormous, about 150,000 light-years across. Just above it lies smaller, bluish NGC 5569. Near center a third galaxy, NGC 5560, is apparently stretched and distorted by its interaction with massive NGC 5566. The trio is also included in Halton Arp's 1966 Atlas of Peculiar Galaxies as Arp 286. Of course, such cosmic interactions are now appreciated as part of the evolution of galaxies.", 'hdurl': 'https://apod.nasa.gov/apod/image/2206/Arp286-202203-CDK24-FLIPL9000-LRGB_NicolasROLLAND_signature_LD.jpg', 'media_type': 'image', 'service_version': 'v1', 'title': 'Arp 286: Trio in Virgo', 'url': 'https://apod.nasa.gov/apod/image/2206/Arp286-202203-CDK24-FLIPL9000-LRGB_NicolasROLLAND_signature_LD1024.jpg'}]

我是python新手,我該怎么做?

您的 JSON 在列表中,因此您需要先選擇它,然后再獲取屬性。

嘗試這個:

url = parsed[0]['url']

看起來 parsed 是一個包含字典的列表。 嘗試解析[0]['url']

或者,我建議您查看 python 的 JSON 模塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM