我用json的loads方法再入一个json字符串,转化后的dict的元素位置和转化前的不一致了。因为我要做排序的测试,所以这个要影响结果。请各位大神给找找原因,或者给个解决方案。
内置模块问题。
1、import json
from collections import OrderedDict
metadata = json.loads(text, object_pairs_hook=OrderedDict);
metadata中properties的顺序是跟text中定义的顺序是一样的。
具体可以看python文档中json.loads函数中的参数
2、可以使用内置模块 json
content = {"id": "evt_ugB6x3K43D16wXCcqbplWAJo"}print content #{'id': 'evt_ugB6x3K43D16wXCcqbplWAJo'}import jsonprint json.dumps(content) #{"id": "evt_ugB6x3K43D16wXCcqbplWAJo"}print type(json.dumps(content)) #<type 'str'>。
参考资料:http://docs.python.org/dev/library/json.html
本回答被提问者和网友采纳