列表标记必须是整数。不是严格的JSON响应

时间:2017-06-02 20:25:50

标签: python json string list parsing

我感到困惑,不知道如何解决这个错误。我试图抓住JSON响应列表中的每个名字。

我的代码看起来像这样。

def extract_strucutres_ids(expected_structures):
    response = requests.get(JIRA_REST + "/structures", verify=False)
    response = response.json()
    for structure in response['structures']:
        print structure['name']

Json的回应看起来像这样。

{
    "structures": [{
            "id": 165,
            "name": "6.2 External Notifications Refactor",
            "description": ""
        }, {
            "id": 364,
            "name": "6.4 Day/Night Mode and Idle Scene Mode",
            "description": "",
            "readOnly": true
        }, {
            "id": 140,
            "name": "ACC 5 Regression",
            "description": ""
        }
    ]
}

我一直在List indicies must be integers, not str。 Python版本2.7.10

2 个答案:

答案 0 :(得分:2)

试试这个 -

 import json

 def extract_strucutres_ids(expected_structures):
    response = requests.get(JIRA_REST + "/structures", verify=False)
    if response.status_code==200:
        response_json = json.loads(response.text)
        for structure in response_json['structures']:
           print structure['name']
    else:
        print("Response is {}".format(response.status_code))

请告诉我,如果有效!

答案 1 :(得分:0)

使用json.loads()

response = requests.get(..)
response = json.loads(response.text) # response.text is a string
for structure in response['structures']: 
   # Do something