2017-06-02 41 views
1

我很困惑,不知道如何解决这个错误。我试图抓住JSON响应列表中的每个名字。列表标记必须是整数。不str str的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

+0

请在'response ['strucutres']'中修正错字。应该是'结构'。 – codekaizer

+1

,而'response'可能会像字典一样,它可能是一个字符串。这可以通过打印'response'类型来确认:'print(type(response))'。 –

回答

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)) 

让我知道,如果它的工作!

+0

它的工作原理!谢谢! –

0

使用json.loads()

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

我得到了错误“期望的字符串或缓冲区”在线响应= json.loads(response.json()) –

+0

@ j.doe,修改我的答案使用'response.text'而不是 – codekaizer

相关问题