import json
from collections import OrderedDict
json_data = '{"test": 1, "test2": 2, "test3":2}'
data = json.loads(json_data, object_pairs_hook=OrderedDict)
json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(data)
本文介绍了一种使用Python中的OrderedDict来保持JSON数据解析后字典顺序的方法。通过指定json模块的loads方法中的object_pairs_hook参数为OrderedDict,可以确保即使在Python的不同运行环境中,解析后的字典顺序也保持一致。
5155

被折叠的 条评论
为什么被折叠?



