我希望通过GET请求可以轻松地解析JSON文件。我是新来的,希望能得到一些帮助。这是我从get请求获得的JSON文件。
{
"Meta Data": {
"1. Information": "Intraday Prices and Volumes for Digital Currency",
"2. Digital Currency Code": "BTC",
"3. Digital Currency Name": "Bitcoin",
"4. Market Code": "CNY",
"5. Market Name": "Chinese Yuan",
"6. Interval": "5min",
"7. Last Refreshed": "2018-05-19 15:50:00",
"8. Time Zone": "UTC"
},
"Time Series (Digital Currency Intraday)": {
"2018-05-19 15:50:00": {
"1a. price (CNY)": "53014.32816681",
"1b. price (USD)": "8311.73569032",
"2. volume": "602.25300624",
"3. market cap (USD)": "5005767.80656960"
},
"2018-05-19 15:45:00": {
"1a. price (CNY)": "53013.58227123",
.......
我想退还最近的任何密码硬币的美元价格,我已经进入。这是“时间序列(数字货币日内)”对象中的第一个对象。我怎么才能在蟒蛇里不吃这个呢?我知道如何进入一个对象,因为我知道它的名字,但由于时间总是在变化,我如何进入第一个对象(即。对象的索引),而不是名称"2018-05-19 15:50:00“。这是与本节相关的代码:
data = {'function' : 'DIGITAL_CURRENCY_INTRADAY', 'symbol' : 'BTC' , 'market' : 'CNY', 'apikey' : APIKey}
r = requests.get('https://www.alphavantage.co/query?',data)
data = r.json()
symbol = data['Meta Data']['2. Digital Currency Code'] #this works fine
print(symbol)
price = data['Time Series (Digital Currency Intraday)'] #how do I keep going in an say "the first index of Time Series.."?
print(price)
我知道这不应该很难,但是我已经在互联网上到处找过了,对于如何浏览JSON文件,我无法给出一个明确的答案。谢谢!
发布于 2018-05-19 20:02:40
您可以使用data['Time Series (Digital Currency Intraday)']
的元素来迭代dict.items()
dated_prices = data['Time Series (Digital Currency Intraday)']
for registered_date, prices in dated_prices.items():
# e.g. for first iteration, "registered_date" contains "2018-05-19 15:50:00"
usd_price = prices["1b. price (USD)"]
print(usd_price)
如果你只想第一次约会:
dated_prices = data['Time Series (Digital Currency Intraday)']
# Sort dates prices from the latest to the earliest
sorted_dated_prices = sorted(dated_prices.items(), key=lambda dp: dp[0], reverse=True)
# Get the latest dated price entry
latest_dated_price = sorted_dated_prices[0]
# Get the price (ignore the date)
latest_price = latest_dated_price[1]
# Get the dollar price
usd_price = latest_price["1b. price (USD)"]
print(usd_price)
发布于 2018-05-19 19:58:35
这能解决你的问题吗?
发布于 2018-05-19 20:03:19
如果您熟悉Python的Pandas,可以尝试使用pandas.read_json
。这会将你的数据放入一个熊猫对象中,从而使传统的咀嚼变得更容易。recent_date = df['StartDate'].max()
https://stackoverflow.com/questions/50428931
复制相似问题