Python 列表标记必须是整数。非str-JSON响应
我很困惑,不知道如何解决这个错误。我试图获取JSON响应列表中的每个名称 我的代码看起来像这样Python 列表标记必须是整数。非str-JSON响应,python,json,string,list,parsing,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']:
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']
{
"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": ""
}
]
}
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']
{
"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": ""
}
]
}
我不断得到列表标记必须是整数,而不是str
。
Python版本2.7.10使用json.loads()
试试这个-
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))
让我知道,如果它工作 请先在
响应['structures']
中修复打字错误。应该是结构
。虽然响应
看起来像字典,但它可能是字符串。这可以通过打印response
:print(type(response))
的类型来确认。我在response=json.loads(response.json())@j.doe行中得到错误“expected string or buffer”。我修改了答案,改为使用response.text