简体   繁体   English

从 Python 中的 JSON 响应中获取键的值

[英]Get a value of key from JSON response in Python

I am accessing an API through a get request from which I get a response.我正在通过获取请求访问 API 并从中获得响应。

This is my code:这是我的代码:

import requests
import json
symbol = "AAPL"
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/"+symbol+"?apiKey=my_key"

r=requests.get(end_point).json()


print(r)
print(r['min'])

print(r) returned: print(r)返回:

{'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}}

but when I try to access the key "min", I get key error:但是当我尝试访问密钥“min”时,出现密钥错误:

KeyError: 'min'

This is super-simple.这是超级简单的。 What am I doing wrong?我究竟做错了什么?

Please try:请试试:

r['ticker']['min']

the key min is inside ticker .关键minticker内。 Do this:做这个:

r['ticker']['min']

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

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