使用以下代码解析来自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"
}
]
答案 0 :(得分:1)
尝试data[0]['e']
,因为原始JSON的顶层是一个列表。