具有多个键和多个要分类的属性的JSON

时间:2019-05-10 15:19:03

标签: python json python-3.x

我目前正在试图弄清楚如何用Python解析JSON信息,并且比我想象的要难。

下面是我要解析的信息示例。

{
  "2019-05-09": {
    "1. open": "124.2900",
    "2. high": "125.7800",
    "3. low": "123.5700",
    "4. close": "125.5000",
    "5. volume": "23491093"
  },
  "2019-05-08": {
    "1. open": "125.4400",
    "2. high": "126.3700",
    "3. low": "124.7500",
    "4. close": "125.5100",
    "5. volume": "25775583"
  },
  "2019-05-07": {
    "1. open": "126.4600",
    "2. high": "127.1800",
    "3. low": "124.2200",
    "4. close": "125.5200",
    "5. volume": "36017661"
  }
}

我试图将每一天都存储到一个类中,以便我可以解析信息。

在下面的示例中,我仅尝试打印这些记录的开头。根据我看过的简单示例,这应该可以工作,但是总是会出现错误“字符串索引必须是整数”。

from alpha_vantage.timeseries import TimeSeries
import json

class day_history:
    def __init__(self, date, open, high, low, close, volume):
        self.date = date
        self.open = open
        self.high = high
        self.low = low
        self.close = close
        self.volume = volume

alpha_adv_key = "aaaaaaaaaaa"

ts = TimeSeries(key=alpha_adv_key)
data, meta_data = ts.get_daily(symbol='MSFT')

results = json.dumps(data)

for day in results:
    print(day["1. open"])

解析此JSON数据以便将其存储在类中的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果要将其转换为类,请参见下面的代码。

话虽如此,我认为您遇到的问题是for day in results:。这将返回每个键(即字符串)。我认为您打算执行for day in results.values():,该操作将返回所有字典。

将json转换为类。

您可以使用**来打开包装。 我将您的输入用作参数d

days = []
for day, info in d.items():
    temp = info.copy() # A copy because we don't want to change the original
    keys = list(temp.keys()) # List of all keys (Ex: 1. open)

    # We want to remove the beginning number so we just have
    # the wanted attribute name(Ex: open instead of 1. open)
    for key in keys:
        temp[key[3:]] = temp.pop(key)
    temp['date'] = day # Add our date to the temp dictionary

    # note the ** notation. This unpacks a dictionary to keyword arguments
    # so it would be like passing: (date=day, open=..., close=..., ...)
    # that is why I had to remove the numbers before the variable name.
    days.append(day_history(**temp)) # Pass this new dict to our constructor

# This will print out all the days
print(days)

我向您的班级添加了一个__repr__方法来打印它们:

    def __repr__(self):
        return str(self.date) + str(self.high)

如果您不需要更多的类方法,则可能只想考虑使用namedtuple

from collections import namedtuple

Day_History = namedtuple("Day_History", "date open high low close volume")
days = []

for day, info in d.items():
    temp = info.copy()
    keys = list(temp.keys())
    for key in keys:
        temp[key[3:]] = temp.pop(key)
    temp['date'] = day
    days.append(Day_History(**temp))

for day in days:
    print(day)

输出:

Day_History(date='2019-05-09', open='124.2900', high='125.7800', low='123.5700', close='125.5000', volume='23491093')
Day_History(date='2019-05-08', open='125.4400', high='126.3700', low='124.7500', close='125.5100', volume='25775583')
Day_History(date='2019-05-07', open='126.4600', high='127.1800', low='124.2200', close='125.5200', volume='36017661')

,您无法通过执行以下操作来打印namedtuple参数:

for day in days:
    print(day.open)