从Python dict到JSON的多个相同键
我正在尝试用Python创建JSON对象,尽管我不能用相同的名称获得多个键,但它工作得很好——但我需要这样做。 这里有一个函数:从Python dict到JSON的多个相同键,python,json,dictionary,Python,Json,Dictionary,我正在尝试用Python创建JSON对象,尽管我不能用相同的名称获得多个键,但它工作得很好——但我需要这样做。 这里有一个函数: findings = AutoTree() findings['report']['numberOfConditions'] = num_cond if r == 'Mammography': f_temp = df['Releva
findings = AutoTree()
findings['report']['numberOfConditions'] = num_cond
if r == 'Mammography':
f_temp = df['Relevant findings'].values.tolist()[0:8]
f_list = [x for i, x in enumerate(f_temp) if i == f_temp.index(x)]
f_num_total = len(f_list)
f_rand = random.randrange(1, f_num_total + 1)
iter_params_mass = ['shape', 'margin', 'density']
for i in range(num_cond):
br = get_birad(row, 2, 7)
cond = camelCase(get_cond_name())
findings[cond]['biRad'] = br
for k in range(f_rand + 1):
f = camelCase(random.choice(f_list))
#f = 'mass'
if f == 'mass':
rep_temp = create_rep(iter_params_mass, row, f, r)
findings[cond][f] = rep_temp
"""I also have a lot elif conditions, and it just grabs parameters."""
report = json.dumps(findings)
print(report)
输出:
{
"report":{
"id":85,
"name":"Lydia",
"age":39,
"relevantModality":"Mammography",
"numberOfConditions":2
},
"ductEctasia":{
"biRad":"birad[1]",
"calcifications":[
{
"typicallyBenign":"Vascular",
"suspiciousMorphology":"Coarse heterogeneous",
"distribution":"Diffuse"
}
],
"lymphNodes":[
{
"lymphNodes":"Lymph nodes \u2013 axillary"
}
]
}
}
我想要多个“淋巴结”和“钙化”物体。可能吗?也许,您可以建议另一种创建JSON对象的方法,而不是嵌套字典?问题是我需要分别为从数据库中选择的随机参数创建对象。您已经使用了“淋巴结”和“钙化”列表,那么是什么阻止您在其中包含多个对象呢?另外,您使用的方法(列表)是在JSON中具有相同键的多个元素的正确方法。它只是不迭代,也不追加。也许您可以共享完整的代码,并尝试更清楚地了解您的问题,因为我不确定您指的是什么或问题是什么。