繁体   English   中英

在JSON列表中搜索字符串

[英]Searching for a string within JSON list

我正在搜索JIRA子任务名称(摘要),但无法弄清楚(发布了单独的问题- 解析JSON文件python )。 到目前为止,没有解决方案,然后我尝试搜索列表中的摘要值(在这种情况下,我有2张票,都创建了子任务:

[{u'fields': {u'status': {u'statusCategory': {u'name': u'To Do', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'id': 2, u'key': u'new', u'colorName': u'blue-gray'}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'issuetype': {u'name': u'Sub-task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10101', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype', u'subtask': True, u'avatarId': 10316, u'id': u'10101', u'description': u'The sub-task of the issue'}, u'summary': u'The specified directory could not be found in the specified region.-traider'}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18938', u'id': u'18938', u'key': u'TECH-1483'}]



[{u'fields': {u'status': {u'statusCategory': {u'name': u'To Do', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'id': 2, u'key': u'new', u'colorName': u'blue-gray'}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'issuetype': {u'name': u'Sub-task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10101', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype', u'subtask': True, u'avatarId': 10316, u'id': u'10101', u'description': u'The sub-task of the issue'}, u'summary': u'The specified directory could not be found in the specified region.-traider'}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18939', u'id': u'18939', u'key': u'TECH-1484'}]

对于两张票,我正在尝试搜索value The specified directory could not be found in the specified region.-traiderThe specified directory could not be found in the specified region.-traider存在。 如果存在,则在summary下,如果不打印,则将其打印出来

response.json() https://1drv.ms/u/s!AizscpxS0QM4hJpJDD8awXdZ9_4dZw

此代码返回,没有错误也没有任何结果:

import os
import csv
import urllib2
import argparse
import json
from bson import json_util

#password = str(sys.argv[1])



headers = {
    'Content-Type': 'application/json',
}

params = (
    ('jql', 'project="Technology" AND summary~"workspace creation*" AND issuetype="Task" AND status!="DONE"'),
)





response = requests.get('https://jira.corp.company.com/rest/api/2/search', headers=headers, params=params, auth=('user', 'pass;))



data = response.json()

for issue in data['issues']:
   summary = issue['fields']['subtasks']
   sub='The specified directory could not be found in the specified region.-traider'
   if sub in summary:
     print sub

您只需要迭代响应

In [41]: print d
{u'expand': u'schema,names', u'total': 2, u'startAt': 0, u'maxResults': 50, u'issues': [{u'expand': u'operations,versionedRepresentations,editmeta,changelog,renderedFields', u'fields': {u'customfield_10700': None, u'creator': {u'avatarUrls': {u'24x24': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24', u'16x16': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16', u'48x48': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48', u'32x32': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32'}, u'displayName': u'user', u'name': u'user', u'key': u'user', u'timeZone': u'Europe/Belgrade', u'active': True, u'emailAddress': u'user@company.com', u'self': u'https://jira.corp.company.com/rest/api/2/user?username=user'}, u'customfield_10600': None, u'aggregatetimeestimate': None, u'labels': [], u'aggregatetimespent': None, u'watches': {u'self': u'https://jira.corp.company.com/rest/api/2/issue/TECH-1433/watchers', u'watchCount': 1, u'isWatching': True}, u'fixVersions': [], u'assignee': None, u'lastViewed': u'2018-04-19T14:43:45.534+0000', u'customfield_11018': None, u'issuelinks': [], u'duedate': None, u'customfield_11019': None, u'customfield_11009': None, u'customfield_11005': None, u'customfield_11004': None, u'votes': {u'hasVoted': False, u'self': u'https://jira.corp.company.com/rest/api/2/issue/TECH-1433/votes', u'votes': 0}, u'customfield_11006': None, u'customfield_11001': None, u'customfield_11000': None, u'customfield_11003': None, u'customfield_11002': None, u'timeoriginalestimate': None, u'description': u'Username:traider1\r\n\r\ndomain:dev.company.net\r\n\r\nFirst Name:tomb1\r\n\r\nLast Name:raider1', u'customfield_10302': None, u'customfield_11100': None, u'customfield_11101': None, u'customfield_11102': None, u'created': u'2018-04-17T12:03:20.000+0000', u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'timespent': None, u'customfield_10000': None, u'progress': {u'progress': 0, u'total': 0}, u'customfield_10108': None, u'customfield_10300': u'com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue@38ef713', u'customfield_11008': None, u'status': {u'statusCategory': {u'colorName': u'blue-gray', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'name': u'To Do', u'key': u'new', u'id': 2}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'updated': u'2018-04-19T06:58:37.000+0000', u'subtasks': [{u'fields': {u'status': {u'statusCategory': {u'colorName': u'blue-gray', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'name': u'To Do', u'key': u'new', u'id': 2}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'issuetype': {u'name': u'Sub-task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10101', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype', u'subtask': True, u'avatarId': 10316, u'id': u'10101', u'description': u'The sub-task of the issue'}, u'summary': u'The specified directory could not be found in the specified region.-traider1'}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18938', u'id': u'18938', u'key': u'TECH-1483'}], u'aggregateprogress': {u'progress': 0, u'total': 0}, u'reporter': {u'avatarUrls': {u'24x24': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24', u'16x16': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16', u'48x48': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48', u'32x32': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32'}, u'displayName': u'user', u'name': u'user', u'key': u'user', u'timeZone': u'Europe/Belgrade', u'active': True, u'emailAddress': u'user@company.com', u'self': u'https://jira.corp.company.com/rest/api/2/user?username=user'}, u'customfield_11007': None, u'customfield_10107': {u'self': u'https://jira.corp.company.com/rest/api/2/customFieldOption/10400', u'id': u'10400', u'value': u'company Internal'}, u'customfield_10301': None, u'versions': [], u'customfield_10103': None, u'customfield_10102': None, u'customfield_10101': [], u'customfield_10100': None, u'customfield_10006': None, u'aggregatetimeoriginalestimate': None, u'customfield_10004': u'0|i013au:', u'customfield_10005': None, u'customfield_10002': None, u'environment': None, u'customfield_10001': None, u'customfield_11016': None, u'customfield_11017': None, u'customfield_11014': None, u'customfield_11015': None, u'customfield_11012': None, u'customfield_11013': None, u'customfield_11010': None, u'customfield_11011': None, u'customfield_10204': None, u'customfield_10205': None, u'customfield_10206': None, u'summary': u'workspace creation-2', u'customfield_10200': None, u'customfield_10201': u'2018-04-19', u'customfield_10202': u'2018-04-19', u'customfield_10203': None, u'project': {u'self': u'https://jira.corp.company.com/rest/api/2/project/10001', u'avatarUrls': {u'24x24': u'https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201', u'16x16': u'https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201', u'48x48': u'https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201', u'32x32': u'https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201'}, u'id': u'10001', u'key': u'TECH', u'name': u'Technology'}, u'components': [], u'issuetype': {u'name': u'Task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10100', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype', u'subtask': False, u'avatarId': 10318, u'id': u'10100', u'description': u'A task that needs to be done.'}, u'resolutiondate': None, u'workratio': -1, u'resolution': None, u'timeestimate': None}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18812', u'id': u'18812', u'key': u'TECH-1433'}, {u'expand': u'operations,versionedRepresentations,editmeta,changelog,renderedFields', u'fields': {u'customfield_10700': None, u'creator': {u'avatarUrls': {u'24x24': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24', u'16x16': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16', u'48x48': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48', u'32x32': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32'}, u'displayName': u'user', u'name': u'user', u'key': u'user', u'timeZone': u'Europe/Belgrade', u'active': True, u'emailAddress': u'user@company.com', u'self': u'https://jira.corp.company.com/rest/api/2/user?username=user'}, u'customfield_10600': None, u'aggregatetimeestimate': None, u'labels': [], u'aggregatetimespent': None, u'watches': {u'self': u'https://jira.corp.company.com/rest/api/2/issue/TECH-1427/watchers', u'watchCount': 1, u'isWatching': True}, u'fixVersions': [], u'assignee': None, u'lastViewed': u'2018-04-19T14:43:07.835+0000', u'customfield_11018': None, u'issuelinks': [], u'duedate': None, u'customfield_11019': None, u'customfield_11009': None, u'customfield_11005': None, u'customfield_11004': None, u'votes': {u'hasVoted': False, u'self': u'https://jira.corp.company.com/rest/api/2/issue/TECH-1427/votes', u'votes': 0}, u'customfield_11006': None, u'customfield_11001': None, u'customfield_11000': None, u'customfield_11003': None, u'customfield_11002': None, u'timeoriginalestimate': None, u'description': u'Username:traider\r\n\r\ndomain:dev.company.net\r\n\r\nFirst Name:tomb\r\n\r\nLast Name:raider', u'customfield_10302': None, u'customfield_11100': None, u'customfield_11101': None, u'customfield_11102': None, u'created': u'2018-04-17T10:24:00.000+0000', u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'timespent': None, u'customfield_10000': None, u'progress': {u'progress': 0, u'total': 0}, u'customfield_10108': None, u'customfield_10300': u'com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue@3860cc5d', u'customfield_11008': None, u'status': {u'statusCategory': {u'colorName': u'blue-gray', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'name': u'To Do', u'key': u'new', u'id': 2}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'updated': u'2018-04-19T07:10:21.000+0000', u'subtasks': [{u'fields': {u'status': {u'statusCategory': {u'colorName': u'blue-gray', u'self': u'https://jira.corp.company.com/rest/api/2/statuscategory/2', u'name': u'To Do', u'key': u'new', u'id': 2}, u'description': u'', u'self': u'https://jira.corp.company.com/rest/api/2/status/10000', u'iconUrl': u'https://jira.corp.company.com/', u'id': u'10000', u'name': u'Backlog'}, u'priority': {u'iconUrl': u'https://jira.corp.company.com/images/icons/priorities/medium.svg', u'self': u'https://jira.corp.company.com/rest/api/2/priority/3', u'name': u'Medium', u'id': u'3'}, u'issuetype': {u'name': u'Sub-task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10101', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10316&avatarType=issuetype', u'subtask': True, u'avatarId': 10316, u'id': u'10101', u'description': u'The sub-task of the issue'}, u'summary': u'The specified directory could not be found in the specified region.-traider'}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18939', u'id': u'18939', u'key': u'TECH-1484'}], u'aggregateprogress': {u'progress': 0, u'total': 0}, u'reporter': {u'avatarUrls': {u'24x24': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=24', u'16x16': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=16', u'48x48': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=48', u'32x32': u'https://secure.gravatar.com/avatar/5d92f3ce51d4a090cdcb9b77ee890989?d=mm&s=32'}, u'displayName': u'user', u'name': u'user', u'key': u'user', u'timeZone': u'Europe/Belgrade', u'active': True, u'emailAddress': u'user@company.com', u'self': u'https://jira.corp.company.com/rest/api/2/user?username=user'}, u'customfield_11007': None, u'customfield_10107': {u'self': u'https://jira.corp.company.com/rest/api/2/customFieldOption/10400', u'id': u'10400', u'value': u'company Internal'}, u'customfield_10301': None, u'versions': [], u'customfield_10103': None, u'customfield_10102': None, u'customfield_10101': [], u'customfield_10100': None, u'customfield_10006': None, u'aggregatetimeoriginalestimate': None, u'customfield_10004': u'0|i013am:', u'customfield_10005': None, u'customfield_10002': None, u'environment': None, u'customfield_10001': None, u'customfield_11016': None, u'customfield_11017': None, u'customfield_11014': None, u'customfield_11015': None, u'customfield_11012': None, u'customfield_11013': None, u'customfield_11010': None, u'customfield_11011': None, u'customfield_10204': None, u'customfield_10205': None, u'customfield_10206': None, u'summary': u'workspace creation', u'customfield_10200': None, u'customfield_10201': u'2018-04-19', u'customfield_10202': u'2018-04-19', u'customfield_10203': None, u'project': {u'self': u'https://jira.corp.company.com/rest/api/2/project/10001', u'avatarUrls': {u'24x24': u'https://jira.corp.company.com/secure/projectavatar?size=small&pid=10001&avatarId=10201', u'16x16': u'https://jira.corp.company.com/secure/projectavatar?size=xsmall&pid=10001&avatarId=10201', u'48x48': u'https://jira.corp.company.com/secure/projectavatar?pid=10001&avatarId=10201', u'32x32': u'https://jira.corp.company.com/secure/projectavatar?size=medium&pid=10001&avatarId=10201'}, u'id': u'10001', u'key': u'TECH', u'name': u'Technology'}, u'components': [], u'issuetype': {u'name': u'Task', u'self': u'https://jira.corp.company.com/rest/api/2/issuetype/10100', u'iconUrl': u'https://jira.corp.company.com/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype', u'subtask': False, u'avatarId': 10318, u'id': u'10100', u'description': u'A task that needs to be done.'}, u'resolutiondate': None, u'workratio': -1, u'resolution': None, u'timeestimate': None}, u'self': u'https://jira.corp.company.com/rest/api/2/issue/18806', u'id': u'18806', u'key': u'TECH-1427'}]}

In [42]: for issue in d['issues']:
    ...:     for subtask in issue['fields']['subtasks']:
    ...:         if subtask['fields']['summary'] == 'The specified directory could not be found in the specified region.-traider':
    ...:             print(subtask['fields']['summary'])
    ...:
The specified directory could not be found in the specified region.-traider

In [43]:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM