【发布时间】:2020-09-06 16:35:56
【问题描述】:
我有一个场景,我通过操作 json 有效负载(data2)来替换有效负载(data1)中的值。
数据2:
[
{
"eqid": 71430,
"tags": [
{
"id": 135853,
"content": "content1",
"class_id": 13733,
"class_name": "reEs"
},
{
"id": 1358341,
"content": "content2",
"class_id": 13734447,
"class_name": "reEp"
},
{
"id": 135832561,
"content": "content3",
"class_id": 137342347,
"class_name": "reEj"
},
{
"id": 1358234561,
"content": "content4",
"class_id": 137123347,
"class_name": "reEk"
},
{
"id": 1355678561,
"content": "content5",
"class_id": 137432347,
"class_name": "reEm"
},
{
"id": 1352348561,
"content": "content6",
"class_id": 137786347,
"class_name": "reEn"
}
],
"auth": false
},
{
"eqid": 243582,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13584567561,
"content": "content2",
"class_id": 13745347,
"class_name": "reEt"
},
{
"id": 1353218561,
"content": "content3",
"class_id": 137980347,
"class_name": "reEf"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
},
{
"eqid": 243672,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
}
]
数据 1 -
data1 = {
"data": [
{
"name": "app-pp",
"ck_name": "App1",
"eid": 71430,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
},
{
"name": "app-pq",
"ck_name": "App2",
"eid": 243582,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
}
]
}
现在这里基于如果data1的eid等于eqiddata2的条件
然后将这两个键的tgs 和sid 的有效负载data1 的值替换为键的content(在标签下)和auth 的data2 的值。
我尝试过的:
for tes in data2:
tempmed = tes["eqid"]
tempservice = tes["auth"]
tempservicel = tes["tags"]
for k in data1:
templand= tempkey["name"]
temphck= tempkey["ck_name"]
tempevalid= tempkey["eid"]
tempiaas= tempkey["Iar"]
tempspc= tempkey["sps"]
temptag= tempkey["tas"]
tempserv= tempkey["sid"]
templc = tempkey["last_checked"]
if tempmed == tempevalid:
tempserv = tempservice
temptag = tempservicel
data1.append({'name': templand, 'ck_name': temphck, 'eid': tempevalid, 'Iar': tempiaas, 'sps': tempspc, 'tgs': temptag, 'sid': tempserv, 'last_checked': templc})
我不确定应该采用什么方法来实现这一点,因为我目前的方法不能按预期工作。
预期的 O/P:
{"data":[
{
"name":"app-pp",
"ck_name":"App1",
"eid":71430,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4,content5,content6",
"sid":"false",
"last_checked":"19-05-2020"
},
{
"name":"app-pq",
"ck_name":"App2",
"eid":243582,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4",
"sid":"false",
"last_checked":"19-05-2020"
}
]}
任何帮助都会很棒!
【问题讨论】:
-
如果你得到
for k in data1,那么你应该使用kisdie循环。或者你应该得到for k in data1["data"]:并检查k["eid"] != ...添加替换k['sid'] = test['sid']。而这一切都没有append()
标签: python json python-3.x python-2.7