需要实现得需求:获取0时区(UTC)的时间;可以加减秒数;时间格式为 yyyy-MM-dd’T’HH:mm:ss.SSS

1
2
3
4
5
6
7
import datetime
now = datetime.datetime.utcnow()
delta = datetime.timedelta(seconds=30) #加30秒
n_seconds = now + delta
start = n_seconds.isoformat() #把时间格式化
startTime = str(start)[:-3]+'Z' #去掉最后的三个字符,加上Z
print (startTime) #2020-11-17T09:23:38.278Z

从接口返回的数据中通过某一个字段找出其它字段的值,再该需求中知道name字段的值是多少,通过name字段找出其它key的value。name不重复。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import request
import json

headers = {'content-type': 'application/json','token':'test_token'}
url = 'http://test.com/test/index'
rs = request.get(url,headers = headers)
jsonTest = json.loads(rs.text)
'''
/例如,上面接口返回json结构如下:
{
{
"total":10,
"pageSize":100,
"pageNum":1,
"data":[
{
"id":"10001",
"name":"我是测试数",
"status":"已发布",
"objectName":"我是测试数据-name",
"Fqn":"test.data2"
},
{
"id":"10002",
"name":"newdata",
"status":"PUBLISHED",
"objectName":"新的数据",
"Fqn":"test.data2"
}
],
"pages":2
}
}
'''
test_data = jsonTest.get('data') #获取到data下的所有数据,list类型
targetDate = [] #这里也不一定用list,后面再看,总之先实现了功能
for i in test_data:
name = i.get('name') #i是dict,可以通过key获取到value
if name == '新的数据':
targetDate.append(i['name']) #把字典里value添加到list中
targetDate.append(i['objectName'])
targetDate.append(i['Fqn'])
targetDate.append(i['id'])
#通过上面的数据生成新的json结构
#新的json结构如下,先这么写着,还没改造好。
action = {
"id":targetDate[3],
"name":"随便取一个",
"startTime":startTime,
"endTime":endTime,
"objectFqn":targetDate[2],
"objectName":targetDate[1],
"objectEntityName":targetDate[0]
}

生成多层json结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#生成如下json格式
'''
{
"OpportunityId":"507384",
"ruleContent":{
"root":{
"nodeType":"OPERATOR",
"nodeOperator":{
"operator":"AND"
},
"children":[
{
"nodeType":"OPERATOR",
"nodeOperator":{
"operator":"开头是"
},
"children":[
{
"nodeType":"PROPERTY",
"nodeProperty":{
"dataType":{
"dataTypeBasis":"Email",
"dataTypeRestrict":"SINGLE"
},
"conditionId":"500685",
"conditionName":"StringEmail"
}
},
{
"nodeType":"VALUE",
"nodeValue":{
"value":{
"type":{
"dataTypeBasis":"String",
"dataTypeRestrict":"SINGLE"
},
"value":"口头"
}
}
}
]
},
{
"nodeType":"OPERATOR",
"nodeOperator":{
"operator":"EQ"
},
"children":[
{
"nodeType":"PROPERTY",
"nodeProperty":{
"dataType":{
"dataTypeBasis":"Url",
"dataTypeRestrict":"SINGLE"
},
"conditionId":"500679",
"conditionName":"StringUrl"
}
},
{
"nodeType":"VALUE",
"nodeValue":{
"value":{
"type":{
"dataTypeBasis":"String",
"dataTypeRestrict":"SINGLE"
},
"value":"ereger"
}
}
}
]
}
]
},
"expression":"[{"rootOperator":"AND","operator":"starts with","conditionName":"StringEmail","firstValue":"口头","lastValue":"","conditionNameI18n":{"zh-CN":"StringEmail"}},{"rootOperator":"AND","operator":"=","conditionName":"StringUrl","firstValue":"ereger","lastValue":"","conditionNameI18n":{"zh-CN":"StringUrl"}}]"
}
}
'''
import json

conditionJson = {}
data = json.loads(json.dumps(conditionJson))
OpportunityId = '507372'
data['OpportunityId'] = OpportunityId

ruleContent = {'root': {} ,'expression': []}
data['ruleContent'] = ruleContent

root = {'nodeType':"OPERATOR",'nodeOperator':{ 'operator':'AND' },'children':[]}
data['ruleContent']['root'] = root


#先生产这个
conditionId =''
openid = ''
dataTypeBasis = ''
value = ''
children = [{'nodeType':'PROPERTY','nodeProperty':{'dataType':{'dataTypeBasis':dataTypeBasis,'dataTypeRestrict':'SINGLE'},'conditionId':conditionId,'conditionName':openid}},{'nodeType':'VALUE','nodeValue':{'value':{'type':{'dataTypeBasis':dataTypeBasis,'dataTypeRestrict':'SINGLE'},'value':value}}}]

operator = '开头是'
children = [{'nodeType':'OPERATOR','nodeOperator':{'operator': operator},'children':children},{'nodeType':'OPERATOR','nodeOperator':{'operator': operator},'children':[]}]

data['ruleContent']['root']['children'] = children

lconditionJson = json.dumps(data, ensure_ascii=False)
print(lconditionJson)