【发布时间】:2020-12-06 08:53:42
【问题描述】:
如何在python中追加字典列表?
我正在尝试使用从 MongoDB 检索数据的 python 创建 JSON 数据。 我需要获取以下格式的 JSON 数据。 注意:我从 db 中检索了所有必需的数据。我不确定是否以所需的 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":[
{
"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'] 中的花括号。
简而言之
"System_Details":
{
"Details":[
{
...
...
"job":[
{
...
},
{
...
}
]
}
]}
如何将 "job":[] 附加到 "Details":[] 中?
提前致谢。
【问题讨论】:
-
你的问题不清楚
-
你发布的json无效
标签: python json list dictionary append