如何在python中附加字典列表?
我正在尝试使用python创建JSON数据,其中数据是从MongoDB检索的。我需要以以下格式获取JSON数据。注意:我从db.I检索了所有必需的数据,我不确定是否以所需的JSON格式追加该数据。
JSON数据:
"System_Details":
{
"System_id":"001",
"Details":[
{
"name":"Job-Info"
"job":[
{
"category":"1",
{
"eid":"01",
"role":"associate-1"
},
{
"eid":"02",
"role":"associate-2"
},
{
"eid";"03",
"role":"associate-3"
}
},
{
"category":"2",
{
"eid":"04",
"role":"associate-4"
},
{
"eid":"05",
"role":"associate-5"
},
{
"eid";"06",
"role":"associate-6"
}
},
]
}
]}我的脚本:
System_Details = {}
System_Details['Details'] = []
job = []
job_dict = {}
System_Details["System_id"]= <SYSTEMID ## which is retrieved from db>
job.append(job_dict) #job_dict is having above json values which is mentioned inside job list("job":[])现在job =包含
"job":[
{
"category":"1",
{
"eid":"01",
"role":"associate-1"
},
{
"eid':"02",
"role":"associate-2"
},
{
"eid";"03",
"role":"associate-3"
}
},
{
"category":"2",
{
"eid":"04",
"role":"associate-4"
},
{
"eid':"05",
"role":"associate-5"
},
{
"eid";"06",
"role":"associate-6"
}
},
]如何将此列表附加到System_Details'Details‘
请注意System_Details 'Details‘中的花括号。
In
"System_Details":
{
"Details":[
{
...
...
"job":[
{
...
},
{
...
}
]
}
]}如何将“职务”:[]附加到“细节”:[]?
提前谢谢。
发布于 2020-08-17 09:41:49
既然问题还不清楚,请根据给定的理解来回答这个问题。
"System_Details":
{
"Details":[
{
...
...
"job":[
{
...
},
{
...
}
]
}
]
}因此,有两个列表Details和job。您希望将job中的所有项追加到Details列表中。
您可以在python中使用扩展()将列表附加到另一个列表。
>>> l = [1, 2]
>>> l.extend([3, 4])
>>> l
[1, 2, 3, 4]
>>> l.extend('foo')
>>> l
[1, 2, 3, 4, 'f', 'o', 'o']
>>> l.extend((5, 6))
>>> l
[1, 2, 3, 4, 'f', 'o', 'o', 5, 6]
>>> l.extend({'x': 100, 'y': 200})
>>> l
[1, 2, 3, 4, 'f', 'o', 'o', 5, 6, 'y', 'x']如果回答了你的问题,请更新你的问题或评论。
https://stackoverflow.com/questions/63447140
复制相似问题