在json(Python)中解析代码

2024-09-22 10:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

from googlefinance import getQuotes
import simplejson as json

print (json.dumps(getQuotes('FRA:BMW'), indent=2))
b=(json.dumps(getQuotes('FRA:BMW'), indent=2))
print(type(b))
a = json.loads((json.dumps(getQuotes('FRA:BMW'), indent=2)))
print(type(a))

这就是我得到的:

[
  {
    "LastTradePrice": "73.39",
    "LastTradeWithCurrency": "€73.39",
    "LastTradeDateTime": "2016-05-13T19:57:30Z",
    "LastTradeDateTimeLong": "May 13, 7:57PM GMT+2",
    "ID": "10224532",
    "Index": "FRA",
    "StockSymbol": "BMW",
    "LastTradeTime": "7:57PM GMT+2"
  }
]
<class 'str'>
<class 'list'>
[{'LastTradePrice': '73.39', 'LastTradeWithCurrency': '&#8364;73.39', 'LastTradeDateTimeLong': 'May 13, 7:57PM GMT+2', 'LastTradeDateTime': '2016-05-13T19:57:30Z', 'ID': '10224532', 'Index': 'FRA', 'StockSymbol': 'BMW', 'LastTradeTime': '7:57PM GMT+2'}]
test
Traceback (most recent call last):
  line 11, in <module>
    print((a)["Index"])

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

如您所见,我无法打印“索引”值(在案例:FRA)(脚本的最后一行代码)不知道该怎么做。你知道吗


Tags: 代码importjsonindextypeprintindentgmt