如何解决TypeError:字符串索引必须是整数

时间:2017-06-18 08:13:02

标签: python json python-2.7 nested

我正在尝试访问特定的json数据,下面是代码。即时通讯使用python 2.7。但它出现错误 TypeError:字符串索引必须是整数:

http = win32com.client.Dispatch('MSXML2.XMLHTTP.6.0')
http.open('POST', 'url', False, username, password)
http.setRequestHeader("Content-type"    , "text/json"             )
http.setRequestHeader("User-Agent"      , "Your User Agent String")
http.setRequestHeader("Accept-Encoding" , "gzip, deflate"         )
http.setRequestHeader("Content-Encoding", "gzip, deflate"         )
global data
http.send(json)

global data
data = http.responseText
#data=data.split("{}{}") -try to make json data print nicely but failed.
print data['responses'][0]['status']

我的json数据如下

{
    "responses": [
        {
            "tran_id": "1234",
            "UUID": "122262d7-85f8-4e53-af98-865627cb82cb",
            "status": "success",
            "messages": [],
            "result_params": {
                "record_count": "1"
            },
            "result_table": [
                {
                    "id": "101160766",
                    "rev": "13",
                    "hierarchy_id": "F80005AF46FF0000B5EA6FE06078159E80",
                    "is_current": "1"}}

2 个答案:

答案 0 :(得分:0)

你错了JSON,最后遗漏了一些括号。

"is_current": "1"}}

应该改为

"is_current": "1"}]}]}

<强> >>Demo<<

答案 1 :(得分:0)

a)您的&#34;内容类型&#34;标题字段不正确。它应该是application / json。

b)您的内容编码&#34;标题字段可能不正确。它表示您正在发送两个 deflate和gzip编码的数据。