如何在不知道我在 Python 中查找的对象名称的情况下解析 JSON 文件?

瑞安·贝利斯

我希望通过 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",
.......

我想返回我输入的任何加密货币的最新美元价格。这是“时间序列(日内数字货币)”对象中的第一个对象。我如何在 python 中注释这个?鉴于我知道名称,我知道如何进入一个对象,但由于时间总是在变化,我如何进入第一个(即对象的索引)而不是名称“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 文件给出明确的答案。谢谢!

皮埃尔

您可以遍历data['Time Series (Digital Currency Intraday)']using的元素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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不知道编码的情况下在Python 3中将字节写入文件?

如何在不知道完整文件路径的情况下访问文件?

如何在C ++中不知道文件长度的情况下从文件中读取2D数组?

如何在不知道文件类型或文件名的情况下下载文件?

在不知道文件名的情况下,如何在Powershell中获取文件的完整路径?

如何在不知道文件扩展名的情况下实例化java.io.File对象?

如何在不知道diff文件来自的原始版本的情况下应用补丁?

如何在不知道Asp.Net Core中正确名称的情况下包含js文件

如何在不知道列名的情况下读取固定宽度的文件?

我不知道如何解析这个嵌套的json

Python Beautifulsoup:如何在不知道相应属性名称的情况下通过属性值查找标签?

在Python中循环,因为我不知道如何

如何在不知道对象名称的情况下访问对象

如何在不知道json键的情况下使用JsonReader从json读取值

Delphi-JSON对象-如何在不知道属性名称的情况下迭代所有属性?

如何在不知道JSON值的情况下动态解析某些JSON?

如何在不知道对象名称的情况下返回Json objects子对象?

我不知道如何使用AngularJS在JSON文件中获取子数据

如何在不知道流精度的情况下在文件中写入双精度值

如何在不知道VB.NET中数字键名称的情况下从JSON获取值

当我不知道JSON对象名称时,如何在MVC控制器中获取JSON数据?

如何在不知道其名称的情况下包括Protobuf内置的任意文件集?

如何在不知道键的情况下使用 javascript 从多级 JSON 中获取值

nlohmann 在不知道密钥的情况下解析 json 文件

在 JSON 对象中,如何在不知道键的情况下检索 JSON 对象中对象的值?

如何在 Python 中創建我不知道數量的對象?

如何在不知道结构的情况下在 C# 中使 JSON 对象脱轨

如何在不知道文件名的情况下解析 XML 文件

如何在不知道位置的情况下从嵌套的 json 返回特定的键值对?