在Python中解析json时出现错误信息

时间:2017-02-19 08:04:12

标签: python json web-scraping

使用以下代码解析来自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"
  }
]

1 个答案:

答案 0 :(得分:1)

尝试data[0]['e'],因为原始JSON的顶层是一个列表。