【问题标题】:How to display all string match on JSON output using python如何使用python在JSON输出上显示所有字符串匹配
【发布时间】:2019-08-29 21:58:07
【问题描述】:

您好 stackoverflow 社区,请原谅我。我是 python 新手,所以我真的需要你的帮助。

首先,我使用 curl GET 命令从 JIRA 中提取数据,然后将其转换为 JSON 格式。之后我写了一些代码,只想显示我需要的数据。

import requests
import json
url ="-----"
auth='------', '------'
r = requests.get(url, auth=(auth))
data = r.json()
json_str = json.dumps(data)
resp = json.loads(json_str)
print(resp['values'][0]['fields']['summary'])

输出是这样的:

PS C:\Users\IanJayloG\Desktop\Python Files\Ex_Files_Learning_Python\Exercise Files> & C:/Users/IanJayloG/AppData/Local/Programs/Python/Python37-32/python.exe "c:/Users/IanJayloG/Desktop/Python Files/Ex_Files_Learning_Python/Exercise Files/Test/Untitled-1.py"
Please grant me full access to BBOXX Togo ERP

我只得到一个“摘要:请授予我对 BBOXX Togo ERP 的完全访问权限” 我需要的是在 JSON 输出中获取所有“摘要:”或“键”。

GET 的输出是这样的。

{
    "size": 7,
    "start": 0,
    "limit": 50,
    "isLastPage": true,
    "_links": {
        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/servicedesk/5/queue/213/issue",
        "base": "https://bboxxltd.atlassian.net",
        "context": ""
    },
    "values": [
        {
            "id": "65857",
            "self": "https://bboxxltd.atlassian.net/rest/api/2/issue/65857",
            "key": "CMS-5947",
            "fields": {
                "summary": "Please grant me full access to BBOXX Togo ERP",
                "customfield_10700": [],
                "customfield_10800": {
                    "_links": {
                        "jiraRest": "https://bboxxltd.atlassian.net/rest/api/2/issue/65857",
                        "web": "https://bboxxltd.atlassian.net/servicedesk/customer/portal/5/CMS-5947",
                        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/request/65857"
                    },
                    "requestType": {
                        "_expands": [
                            "field"
                        ],
                        "id": "75",
                        "_links": {
                            "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/servicedesk/5/requesttype/75"
                        },
                        "name": "Accounts, Permissions, Access",
                        "description": "If you're having any issues with your account, or need to change permissions for a user, select this option. (JIRA, ERP, Pulse)",
                        "helpText": "Forgotten your password? Have you tried resetting your password using [user.bboxx.uk|http://user.bboxx.co.uk]  Need access to ERP? Before submitting a ticket try logging into ERP with your Outlook credentials. Need an account created? If you are a manager you should have access to creating accounts for new staff members in user.bboxx.co.uk",
                        "issueTypeId": "10400",
                        "serviceDeskId": "5",
                        "groupIds": [
                            "17"
                        ],
                        "icon": {
                            "id": "13628",
                            "_links": {
                                "iconUrls": {
                                    "48x48": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=large",
                                    "24x24": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=small",
                                    "16x16": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=xsmall",
                                    "32x32": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=medium"
                                }
                            }
                        }
                    },
                    "currentStatus": {
                        "status": "Waiting for support",
                        "statusCategory": "INDETERMINATE",
                        "statusDate": {
                            "iso8601": "2019-08-28T16:51:21+0100",
                            "jira": "2019-08-28T16:51:21.127+0100",
                            "friendly": "Today 4:51 PM",
                            "epochMillis": 1567007481127
                        }
                    }
                },
                "created": "2019-08-28T16:51:21.127+0100",
                "customfield_10805": {
                    "id": "13",
                    "name": "Time to resolution",
                    "_links": {
                        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/request/65857/sla/13"
                    },
                    "completedCycles": [],
                    "ongoingCycle": {
                        "startTime": {
                            "iso8601": "2019-08-28T16:51:21+0100",
                            "jira": "2019-08-28T16:51:21.331+0100",
                            "friendly": "Today 4:51 PM",
                            "epochMillis": 1567007481331
                        },
                        "breachTime": {
                            "iso8601": "2019-09-11T16:51:21+0100",
                            "jira": "2019-09-11T16:51:21.331+0100",
                            "friendly": "11/Sep/19 4:51 PM",
                            "epochMillis": 1568217081331
                        },
                        "breached": false,
                        "paused": false,
                        "withinCalendarHours": false,
                        "goalDuration": {
                            "millis": 288000000,
                            "friendly": "80h"
                        },
                        "elapsedTime": {
                            "millis": 518669,
                            "friendly": "8m"
                        },
                        "remainingTime": {
                            "millis": 287481331,
                            "friendly": "79h 51m"
                        }
                    }
                },
                "customfield_10806": {
                    "id": "14",
                    "name": "Time to first response",
                    "_links": {
                        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/request/65857/sla/14"
                    },
                    "completedCycles": [
                        {
                            "startTime": {
                                "iso8601": "2019-08-28T16:51:21+0100",
                                "jira": "2019-08-28T16:51:21.331+0100",
                                "friendly": "Today 4:51 PM",
                                "epochMillis": 1567007481331
                            },
                            "stopTime": {
                                "iso8601": "2019-08-28T16:55:10+0100",
                                "jira": "2019-08-28T16:55:10.131+0100",
                                "friendly": "Today 4:55 PM",
                                "epochMillis": 1567007710131
                            },
                            "breached": false,
                            "goalDuration": {
                                "millis": 86400000,
                                "friendly": "24h"
                            },
                            "elapsedTime": {
                                "millis": 228800,
                                "friendly": "3m"
                            },
                            "remainingTime": {
                                "millis": 86171200,
                                "friendly": "23h 56m"
                            }
                        }
                    ]
                },
                "issuelinks": [],
                "reporter": {
                    "self": "https://bboxxltd.atlassian.net/rest/api/2/user?accountId=5ab4cc718835f42a65099826",
                    "name": "p.emungu",
                    "key": "p.emungu",
                    "accountId": "5ab4cc718835f42a65099826",
                    "emailAddress": "p.emungu@bboxx.co.uk",
                    "avatarUrls": {
                        "48x48": "https://secure.gravatar.com/avatar/b2040c0078460d5cfab1b7b372239234?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FPE-4.png&size=48&s=48",
                        "24x24": "https://secure.gravatar.com/avatar/b2040c0078460d5cfab1b7b372239234?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FPE-4.png&size=24&s=24",
                        "16x16": "https://secure.gravatar.com/avatar/b2040c0078460d5cfab1b7b372239234?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FPE-4.png&size=16&s=16",
                        "32x32": "https://secure.gravatar.com/avatar/b2040c0078460d5cfab1b7b372239234?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FPE-4.png&size=32&s=32"
                    },
                    "displayName": "Patrick Emungu",
                    "active": true,
                    "timeZone": "Europe/London",
                    "accountType": "atlassian"
                },
                "updated": "2019-08-28T16:55:14.956+0100",
                "status": {
                    "self": "https://bboxxltd.atlassian.net/rest/api/2/status/11101",
                    "description": "This was auto-generated by JIRA Service Desk during workflow import",
                    "iconUrl": "https://bboxxltd.atlassian.net/images/icons/status_generic.gif",
                    "name": "Waiting for support",
                    "id": "11101",
                    "statusCategory": {
                        "self": "https://bboxxltd.atlassian.net/rest/api/2/statuscategory/4",
                        "id": 4,
                        "key": "indeterminate",
                        "colorName": "yellow",
                        "name": "In Progress"
                    }
                }
            }
        },
        {
            "id": "65852",
            "self": "https://bboxxltd.atlassian.net/rest/api/2/issue/65852",
            "key": "CMS-5945",
            "fields": {
                "summary": "ERP Databases access with Read Only",
                "customfield_10700": [
                    {
                        "id": "3",
                        "name": "London",
                        "_links": {
                            "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/organization/3"
                        }
                    }
                ],
                "customfield_10800": {
                    "_links": {
                        "jiraRest": "https://bboxxltd.atlassian.net/rest/api/2/issue/65852",
                        "web": "https://bboxxltd.atlassian.net/servicedesk/customer/portal/5/CMS-5945",
                        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/request/65852"
                    },
                    "requestType": {
                        "_expands": [
                            "field"
                        ],
                        "id": "75",
                        "_links": {
                            "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/servicedesk/5/requesttype/75"
                        },
                        "name": "Accounts, Permissions, Access",
                        "description": "If you're having any issues with your account, or need to change permissions for a user, select this option. (JIRA, ERP, Pulse)",
                        "helpText": "Forgotten your password? Have you tried resetting your password using [user.bboxx.uk|http://user.bboxx.co.uk]  Need access to ERP? Before submitting a ticket try logging into ERP with your Outlook credentials. Need an account created? If you are a manager you should have access to creating accounts for new staff members in user.bboxx.co.uk",
                        "issueTypeId": "10400",
                        "serviceDeskId": "5",
                        "groupIds": [
                            "17"
                        ],
                        "icon": {
                            "id": "13628",
                            "_links": {
                                "iconUrls": {
                                    "48x48": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=large",
                                    "24x24": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=small",
                                    "16x16": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=xsmall",
                                    "32x32": "https://bboxxltd.atlassian.net/secure/viewavatar?avatarType=SD_REQTYPE&avatarId=13628&size=medium"
                                }
                            }
                        }
                    },
                    "currentStatus": {
                        "status": "Escalated",
                        "statusCategory": "INDETERMINATE",
                        "statusDate": {
                            "iso8601": "2019-08-28T16:48:52+0100",
                            "jira": "2019-08-28T16:48:52.245+0100",
                            "friendly": "Today 4:48 PM",
                            "epochMillis": 1567007332245
                        }
                    }
                },
                "created": "2019-08-28T16:48:52.245+0100",
                "customfield_10805": {
                    "id": "13",
                    "name": "Time to resolution",
                    "_links": {
                        "self": "https://bboxxltd.atlassian.net/rest/servicedeskapi/request/65852/sla/13"
                    },
                    "completedCycles": [],
                    "ongoingCycle": {
                        "startTime": {
                            "iso8601": "2019-08-28T16:48:52+0100",
                            "jira": "2019-08-28T16:48:52.408+0100",
                            "friendly": "Today 4:48 PM",
                            "epochMillis": 1567007332408
                        },

我需要的输出是"summary:" "key:" and "web:"。 即:

"summary": "Please grant me full access to BBOXX Togo ERP"
"summary": "ERP Databases access with Read Only"
"summary": "new starter: Edoardo Bologna"
"key": etc
"key": etc
"web": etc
"web": etc

【问题讨论】:

    标签: python json python-3.x get python-requests


    【解决方案1】:

    我相信您需要一个 for 循环来遍历列表“值”中的所有项目:

    for i in range(0,len(resp['values'])):
           print(resp['values'][i]['fields']['summary'])
    for i in range(0,len(resp['values'])):
           print(resp['values'][i]['key'])
    for i in range(0,len(resp['values'])):
          print(resp['values'][i]['fields']['customfield_10800']['_links']['web'])
    

    我要做的是创建一个字典并附加这些值:

    information = {'summary':[],'key':[],'web':[]}
    for i in range(0,len(resp['values'])):
         information['summary'].append(resp['values'][i]['fields']['summary'])
         information['key'].append(resp['values'][i]['key'])
         information['web'].append(resp['values'][i]['fields']['customfield_10800']['_links']['web'])
    

    【讨论】:

    • 嗨@Celius Stingher,感谢您的回复。我有这个错误:for i in range(0,len(resp['values']): ^ SyntaxError: invalid syntax
    • 哦,缺少括号,正在编辑我的答案。关闭len函数,忘记关闭range函数。
    • 我很高兴它做到了,如果有其他内容弹出,请在评论中告诉我!编码愉快。
    • 谢谢。对此,我真的非常感激! :)
    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2016-10-24
    • 2013-03-08
    • 1970-01-01
    • 2013-06-22
    相关资源
    最近更新 更多