【发布时间】:2017-02-19 08:04:11
【问题描述】:
我使用下面的代码来解析来自 url 的 json。但是我收到了这个错误:
ex= data['e']
TypeError: list indices must be integers or slices, not str
这是我的代码:
html = urlopen("http://localhost:8080/messenger/webapi/myresource")
content = html.read()
data = json.loads(content.decode("utf8"))
ex = data['e']
print(ex)
这是 JSON:
[
{
"c":"-1.00",
"c_fix":"-1.00",
"ccol":"chr",
"cp":"-0.37",
"cp_fix":"-0.37",
"e":"NSE",
"id":"13564339",
"l":"269.50",
"l_cur":"₹269.50",
"l_fix":"269.50",
"lt":"Feb 17, 3:56PM GMT+5:30",
"lt_dts":"2017-02-17T15:56:16Z",
"ltt":"3:56PM GMT+5:30",
"pcls_fix":"270.5",
"s":"0",
"t":"SBIN"
}
]
【问题讨论】:
-
解析的结果是一个列表,然后你使用
data['e']访问它,因此错误,尝试data[0]['e'] -
data[0]['e'];或使用for item in data: item['e']
标签: python json web-scraping