解析 JSON Github API - Django 1.8
Parsing JSON Github API - Django 1.8
我的 views.py
里有这个:
def profile(request):
parsedData = []
if request.method == 'POST':
username = request.POST.get('user')
req = requests.get('https://api.github.com/users/' + username + '/repos')
jsonList = []
jsonList.append(req.json())
userData = {}
data = ['owner']
for data in jsonList:
userData['html_url'] = data['owner'][0]['html_url']
userData['created_at'] = data['created_at']
userData['updated_at'] = data['updated_at']
userData['forks_count'] = data['forks_count']
parsedData.append(userData)
return render(request, 'app/profile.html', {'data': parsedData})
这让我感到震惊:
TypeError at /app/profile/
list indices must be integers, not str
这是 JSON 由 /repos
在 Github 上返回的示例:
{
"id": 77549474,
"name": "acp",
"full_name": "kkoci/acp",
"owner": {
"login": "kkoci",
"id": 3047897,
"avatar_url": "https://avatars0.githubusercontent.com/u/3047897?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/kkoci",
"html_url": "https://github.com/kkoci",
"followers_url": "https://api.github.com/users/kkoci/followers",
"following_url": "https://api.github.com/users/kkoci/following{/other_user}",
"gists_url": "https://api.github.com/users/kkoci/gists{/gist_id}",
"starred_url": "https://api.github.com/users/kkoci/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/kkoci/subscriptions",
"organizations_url": "https://api.github.com/users/kkoci/orgs",
"repos_url": "https://api.github.com/users/kkoci/repos",
"events_url": "https://api.github.com/users/kkoci/events{/privacy}",
"received_events_url": "https://api.github.com/users/kkoci/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/kkoci/acp",
"description": null,
"fork": true,
"url": "https://api.github.com/repos/kkoci/acp",
"forks_url": "https://api.github.com/repos/kkoci/acp/forks",
"keys_url": "https://api.github.com/repos/kkoci/acp/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/kkoci/acp/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/kkoci/acp/teams",
"hooks_url": "https://api.github.com/repos/kkoci/acp/hooks",
"issue_events_url": "https://api.github.com/repos/kkoci/acp/issues/events{/number}",
"events_url": "https://api.github.com/repos/kkoci/acp/events",
"assignees_url": "https://api.github.com/repos/kkoci/acp/assignees{/user}",
"branches_url": "https://api.github.com/repos/kkoci/acp/branches{/branch}",
"tags_url": "https://api.github.com/repos/kkoci/acp/tags",
"blobs_url": "https://api.github.com/repos/kkoci/acp/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/kkoci/acp/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/kkoci/acp/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/kkoci/acp/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/kkoci/acp/statuses/{sha}",
"languages_url": "https://api.github.com/repos/kkoci/acp/languages",
"stargazers_url": "https://api.github.com/repos/kkoci/acp/stargazers",
"contributors_url": "https://api.github.com/repos/kkoci/acp/contributors",
"subscribers_url": "https://api.github.com/repos/kkoci/acp/subscribers",
"subscription_url": "https://api.github.com/repos/kkoci/acp/subscription",
"commits_url": "https://api.github.com/repos/kkoci/acp/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/kkoci/acp/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/kkoci/acp/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/kkoci/acp/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/kkoci/acp/contents/{+path}",
"compare_url": "https://api.github.com/repos/kkoci/acp/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/kkoci/acp/merges",
"archive_url": "https://api.github.com/repos/kkoci/acp/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/kkoci/acp/downloads",
"issues_url": "https://api.github.com/repos/kkoci/acp/issues{/number}",
"pulls_url": "https://api.github.com/repos/kkoci/acp/pulls{/number}",
"milestones_url": "https://api.github.com/repos/kkoci/acp/milestones{/number}",
"notifications_url": "https://api.github.com/repos/kkoci/acp/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/kkoci/acp/labels{/name}",
"releases_url": "https://api.github.com/repos/kkoci/acp/releases{/id}",
"deployments_url": "https://api.github.com/repos/kkoci/acp/deployments",
"created_at": "2016-12-28T17:23:45Z",
"updated_at": "2016-12-28T17:23:46Z",
"pushed_at": "2016-12-28T13:00:52Z",
"git_url": "git://github.com/kkoci/acp.git",
"ssh_url": "git@github.com:kkoci/acp.git",
"clone_url": "https://github.com/kkoci/acp.git",
"svn_url": "https://github.com/kkoci/acp",
"homepage": null,
"size": 113,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Python",
"has_issues": false,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "8.0"
},
我尝试了很多方法,但错误仍然存在,有什么想法吗?
编辑
错误出现在这一行 userData['html_url'] = data['owner'][0]['html_url']
但也发生在其他行,这只是我尝试过的两种方法的一个例子。错误仍然存在。
问题是您将响应附加到列表中。
然后响应只包含一个 JSON 对象。
因此,您甚至不必循环播放它。直接赋值即可。
data = req.json()
userData = {}
# Copy values right away
userData['html_url'] = data['owner'][0]['html_url']
userData['created_at'] = data['created_at']
userData['updated_at'] = data['updated_at']
userData['forks_count'] = data['forks_count']
然后在您的响应中,如果客户不期望列表,则无需将 userData
附加到列表类型。直接传递为:
return render(request, 'app/profile.html', {'data': userData})
data = ['owner']
表示 data
是一个包含一个字符串的列表 owner
但并不意味着它是一个包含一个键 owner
的字典(正确的应该是 data={"owner": None}
).
所以当你像 data["owner"]
一样访问它时,你试图像字典一样使用它,但它是一个列表。
这就是您收到错误的原因:list indices must be integers, not str
。
来自 github 的响应已经是一个列表,因此您 jsonList
中的第一个元素是一个列表,这就是异常的原因。
更改为:
jsonList=req.json()
它应该可以工作
我的 views.py
里有这个:
def profile(request):
parsedData = []
if request.method == 'POST':
username = request.POST.get('user')
req = requests.get('https://api.github.com/users/' + username + '/repos')
jsonList = []
jsonList.append(req.json())
userData = {}
data = ['owner']
for data in jsonList:
userData['html_url'] = data['owner'][0]['html_url']
userData['created_at'] = data['created_at']
userData['updated_at'] = data['updated_at']
userData['forks_count'] = data['forks_count']
parsedData.append(userData)
return render(request, 'app/profile.html', {'data': parsedData})
这让我感到震惊:
TypeError at /app/profile/
list indices must be integers, not str
这是 JSON 由 /repos
在 Github 上返回的示例:
{
"id": 77549474,
"name": "acp",
"full_name": "kkoci/acp",
"owner": {
"login": "kkoci",
"id": 3047897,
"avatar_url": "https://avatars0.githubusercontent.com/u/3047897?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/kkoci",
"html_url": "https://github.com/kkoci",
"followers_url": "https://api.github.com/users/kkoci/followers",
"following_url": "https://api.github.com/users/kkoci/following{/other_user}",
"gists_url": "https://api.github.com/users/kkoci/gists{/gist_id}",
"starred_url": "https://api.github.com/users/kkoci/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/kkoci/subscriptions",
"organizations_url": "https://api.github.com/users/kkoci/orgs",
"repos_url": "https://api.github.com/users/kkoci/repos",
"events_url": "https://api.github.com/users/kkoci/events{/privacy}",
"received_events_url": "https://api.github.com/users/kkoci/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/kkoci/acp",
"description": null,
"fork": true,
"url": "https://api.github.com/repos/kkoci/acp",
"forks_url": "https://api.github.com/repos/kkoci/acp/forks",
"keys_url": "https://api.github.com/repos/kkoci/acp/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/kkoci/acp/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/kkoci/acp/teams",
"hooks_url": "https://api.github.com/repos/kkoci/acp/hooks",
"issue_events_url": "https://api.github.com/repos/kkoci/acp/issues/events{/number}",
"events_url": "https://api.github.com/repos/kkoci/acp/events",
"assignees_url": "https://api.github.com/repos/kkoci/acp/assignees{/user}",
"branches_url": "https://api.github.com/repos/kkoci/acp/branches{/branch}",
"tags_url": "https://api.github.com/repos/kkoci/acp/tags",
"blobs_url": "https://api.github.com/repos/kkoci/acp/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/kkoci/acp/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/kkoci/acp/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/kkoci/acp/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/kkoci/acp/statuses/{sha}",
"languages_url": "https://api.github.com/repos/kkoci/acp/languages",
"stargazers_url": "https://api.github.com/repos/kkoci/acp/stargazers",
"contributors_url": "https://api.github.com/repos/kkoci/acp/contributors",
"subscribers_url": "https://api.github.com/repos/kkoci/acp/subscribers",
"subscription_url": "https://api.github.com/repos/kkoci/acp/subscription",
"commits_url": "https://api.github.com/repos/kkoci/acp/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/kkoci/acp/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/kkoci/acp/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/kkoci/acp/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/kkoci/acp/contents/{+path}",
"compare_url": "https://api.github.com/repos/kkoci/acp/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/kkoci/acp/merges",
"archive_url": "https://api.github.com/repos/kkoci/acp/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/kkoci/acp/downloads",
"issues_url": "https://api.github.com/repos/kkoci/acp/issues{/number}",
"pulls_url": "https://api.github.com/repos/kkoci/acp/pulls{/number}",
"milestones_url": "https://api.github.com/repos/kkoci/acp/milestones{/number}",
"notifications_url": "https://api.github.com/repos/kkoci/acp/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/kkoci/acp/labels{/name}",
"releases_url": "https://api.github.com/repos/kkoci/acp/releases{/id}",
"deployments_url": "https://api.github.com/repos/kkoci/acp/deployments",
"created_at": "2016-12-28T17:23:45Z",
"updated_at": "2016-12-28T17:23:46Z",
"pushed_at": "2016-12-28T13:00:52Z",
"git_url": "git://github.com/kkoci/acp.git",
"ssh_url": "git@github.com:kkoci/acp.git",
"clone_url": "https://github.com/kkoci/acp.git",
"svn_url": "https://github.com/kkoci/acp",
"homepage": null,
"size": 113,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Python",
"has_issues": false,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "8.0"
},
我尝试了很多方法,但错误仍然存在,有什么想法吗?
编辑
错误出现在这一行 userData['html_url'] = data['owner'][0]['html_url']
但也发生在其他行,这只是我尝试过的两种方法的一个例子。错误仍然存在。
问题是您将响应附加到列表中。 然后响应只包含一个 JSON 对象。
因此,您甚至不必循环播放它。直接赋值即可。
data = req.json()
userData = {}
# Copy values right away
userData['html_url'] = data['owner'][0]['html_url']
userData['created_at'] = data['created_at']
userData['updated_at'] = data['updated_at']
userData['forks_count'] = data['forks_count']
然后在您的响应中,如果客户不期望列表,则无需将 userData
附加到列表类型。直接传递为:
return render(request, 'app/profile.html', {'data': userData})
data = ['owner']
表示 data
是一个包含一个字符串的列表 owner
但并不意味着它是一个包含一个键 owner
的字典(正确的应该是 data={"owner": None}
).
所以当你像 data["owner"]
一样访问它时,你试图像字典一样使用它,但它是一个列表。
这就是您收到错误的原因:list indices must be integers, not str
。
来自 github 的响应已经是一个列表,因此您 jsonList
中的第一个元素是一个列表,这就是异常的原因。
更改为:
jsonList=req.json()
它应该可以工作