我试图用JSON对象的数据构建一个列表。但是,列表的顺序与JSON对象的顺序不匹配,几乎每次运行代码时都会发生变化。你知道吗
{
"project":{
"Projektnamen":{
"Server":{
"Server1":{
...
},
"Server2":{
...
},
"Server3":{
...
}
}
}
}
}
with open('json_data.json') as json_file:
# json CONFIG_JSON Konfigurationsdatei
CONFIG_JSON = json.load(json_file)
for key in CONFIG_JSON["project"]["Projektnamen"]["Server"]:
servernamen.append(key)
预期结果:servernamen=[Server1,Server2,Server3]
但是顺序总是改变。 最后一个结果:servernamen=[Server3,Server1,Server2]
json.loads
将JSON反序列化到dictionary对象中,dictionary对象是未排序的哈希表。你知道吗您可以使用来自
collections
的OrderedDict
对键进行排序。 示例您可以使用^{} 和
json.load
的参数导入已经排序的JSON数据:相关问题 更多 >
编程相关推荐