我正在作为请求发送URL,并且在响应中,我又回到了字典列表,我无法循环浏览这些值。
def profile_model(request):
response = unirest.get(url,header)
#url and header is defined outside the function
contents = response.raw_body
for i in contents:
print i['items']
print i['profiles']
return render(request,"profile_model.html",{})
在调试模式下,我看到
Name:contents
Value:
str: {
"items" : [ 13184519, 13184195, 13183948, 13184350, 13183946, 13184208],
"profiles" : [ "slezyr", "stefek99", "amlib", "vyrotek", "xenophonf", "TheGrumpyBrit"]
}
我正在获得TypeError:字符串索引必须是整数,而不是str。如果我在项目中删除引号,我将获得未定义的变量'项目'
如果您的Reponsons.raw_body是字典,则此代码将起作用。如果其列表确实添加了迭代代码。
def profile_model(request):
response = unirest.get(url,header)
#url and header is defined outside the function
contents = json.loads(response.raw_body)
print contents['items']
print contents['profiles']
return render(request,"profile_model.html",{})