我配置了一个连接到Oracle Service Cloud的REST API POST请求,它以下面的json格式接收响应:
{"count":16,"name":"Report Name","columnNames":["Connection","Complete Name","Login ID","Login Time","Logout Time","IP Direction"],"rows":[["PGALICHI","Robert The IT Guy","3205","2018-01-25 08:52:23","2018-01-25 15:00:50","201.255.56.151"],["PGALICHI","Lucas The other IT Guy","3204","2018-01-25 08:52:21","2018-01-25 15:00:51","201.255.56.151"]],"links":[{"rel":"self","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"},{"rel":"canonical","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"},{"rel":"describedby","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/metadata-catalog/analyticsReportResults","mediaType":"application/schema+json"}]}
此信息将是仅打印行的脚本的输入,我现在需要的是,首先按“登录时间”排序所有行,然后使用“登录时间”筛选所有值“等于或早于具有最后”登录时间“值的变量。
这是我现在用来获取行的代码示例:
class OracleORNHandler:
def __init__(self,**args):
pass
def __call__(self, response_object,raw_response_output,response_type,req_args,endpoint):
if response_type == "json":
output = json.loads(raw_response_output)
for row in output["rows"]:
print_xml_stream(json.dumps(row))
else:
print_xml_stream(raw_response_output)
答案 0 :(得分:0)
这需要更多编码。但是我会解释逻辑方法。 您可能希望稍微改变编码方法。
固定字符串
json_out = '{"count":16,"name":"Report Name","columnNames":["Connection","Complete Name","Login ID","Login Time","Logout Time","IP Direction"],"rows":[["PGALICHI","Robert The IT Guy","3205","2018-01-25 08:52:23","2018-01-25 15:00:50","201.255.56.151"],["PGALICHI","Lucas The other IT Guy","3204","2018-01-25 08:52:21","2018-01-25 15:00:51","201.255.56.151"]],"links":[{"rel":"self","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"},{"rel":"canonical","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"},{"rel":"describedby","href":"https://web--tst1.custhelp.com/services/rest/connect/v1.4/metadata-catalog/analyticsReportResults","mediaType":"application/schema+json"}]}'
dic_json = json.loads(json_out)
rows
转换为词典 登录时间用作值,因为我们将需要它以便稍后进行排序。
使用datetime
将字符串转换为日期
from datetime import datetime
rows_list = dic_json['rows']
d = dict()
for x in rows_list:
datetime_object = datetime.strptime(x[4], '%Y-%m-%d %H:%M:%S')
d[x.__str__()] = datetime_object
注意:由于登录时间可能是唯一的,我选择整个列表作为密钥 即使不确定登录ID在这里是唯一的。
collections.OrderedDict
按时排序。; 即
import collections
# sort by time i.e dictionary values
od2 =collections.OrderedDict(sorted(d.items(), key=lambda t: t[1]))
<强> Input_time 强>
date_time_req = datetime.strptime('2018-01-25 08:52:23', '%Y-%m-%d %H:%M:%S')
for y in od2.keys():
if od2[y] <= date_time_req:
print(y)
此输出为字符串,您可以修剪并将其转换回列表。