在json(Python)中解析代码

在json(Python)中解析代码,python,json,string,indices,Python,Json,String,Indices,我有以下代码: 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

我有以下代码:

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
[
{
“LastTradePrice”:“73.39”,
“使用货币的最新交易”:“73.39”,
“LastTradeDateTime”:“2016-05-13T19:57:30Z”,
“LastTradeDateTimeLong”:“5月13日格林威治标准时间下午7:57+2”,
“ID”:“10224532”,
“索引”:“FRA”,
“StockSymbol”:“BMW”,
“LastTradeTime”:“格林威治标准时间下午7:57+2”
}
]
[{'LastTradePrice':'73.39','LastTradeWithCurrency':'和#8364;73.39','LastTradeDateTimeLong':'5月13日格林威治时间下午7:57+2','LastTradeDateTime':'2016-05-13T19:57:30Z','ID':'10224532','Index':'FRA','StockSymbol':'BMW','LastTradeTime':'7:57格林威治时间+2']
测试
回溯(最近一次呼叫最后一次):
第11行,在
打印((a)[“索引”])
TypeError:列表索引必须是整数或片,而不是str


如您所见,我无法打印“Index”值(在这种情况下为FRA)(脚本的最后一行代码),不知道该如何工作

您不需要
json
模块来读取该数据。它已经是Python了。错误表示您必须为
getQuotes
返回的列表编制索引。因为只有一个元素,所以可以使用
[0]

>>> from googlefinance import getQuotes
>>> fra_bmw = getQuotes('FRA:BMW')
>>> fra_bmw[0]["Index"]
u'FRA'

不需要将变量括在括号中
b=json.dumps(getQuotes('FRA:BMW'),indent=2)
可以工作,但错误表明您无法使用类似
a[“index”]
的字符串访问列表(以
[data]
的形式)。您可以尝试
a[0][“Index”]
非常感谢您的支持!