简体   繁体   English

如何从 JSON 中获取价值?

[英]How can I get value from JSON?

I am trying to get 'url' from JSON which i gain from responce in such way:我试图从 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)

After I run program, I recive this message:运行程序后,我收到此消息:

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

I get JSON looks like this:我得到的 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'}]

I am new in python, how can I do it?我是python新手,我该怎么做?

Your JSON is inside a list, so you need to select that first, before getting the attribute.您的 JSON 在列表中,因此您需要先选择它,然后再获取属性。

Try this:尝试这个:

url = parsed[0]['url']

Looks like parsed is a list which has a dictionary in it.看起来 parsed 是一个包含字典的列表。 Try parsed[0]['url']尝试解析[0]['url']

Alternatively, i'd suggest you look into the JSON module for python或者,我建议您查看 python 的 JSON 模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM