我使用以下python代码
获取JIRA数据如何存储多个密钥的响应(我的示例只显示一个KEY,但通常我会获得大量数据)并打印仅与total,key, customfield_12830, summary
<对应的值< / p>
import requests
import json
import logging
import datetime
import base64
import urllib
serverURL = 'https://jira-stability-tools.company.com/jira'
user = 'username'
password = 'password'
query = 'project = PROJECTNAME AND "Build Info" ~ BUILDNAME AND assignee=ASSIGNEENAME'
jql = '/rest/api/2/search?jql=%s' % urllib.quote(query)
response = requests.get(serverURL + jql,verify=False,auth=(user, password))
print response.json()
response.json()
输出: -
答案 0 :(得分:1)
从您粘贴到pastebin的链接和我看到的json,您的issues
列表包含key, fields(which holds custom fields), self, id, expand
。
您可以简单地遍历此响应并提取所需键的值。你可以去。
data = response.json()
issues = data.get('issues', list())
x = list()
for issue in issues:
temp = {
'key': issue['key'],
'customfield': issue['fields']['customfield_12830'],
'total': issue['fields']['progress']['total']
}
x.append(temp)
print(x)
x 是包含您提到的字段数据的字典列表。如果我某处不清楚或者我所提供的内容不是您想要的,请告诉我。
PS:始终建议您使用 dict.get(&#39; keyname&#39;,无)来获取值,因为您始终可以设置默认值如果找不到密钥,则为value。对于这个解决方案,我没有这样做,因为我只想提供方法。
更新:在您(OP)提到的评论中,它给出了attributerror.Try this code
data = response.json()
issues = data.get('issues', list())
x = list()
for issue in issues:
temp = dict()
key = issue.get('key', None)
if key:
temp['key'] = key
fields = issue.get('fields', None)
if fields:
customfield = fields.get('customfield_12830', None)
temp['customfield'] = customfield
progress = fields.get('progress', None)
if progress:
total = progress.get('total', None)
temp['total'] = total
x.append(temp)
print(x)