當前位置:成語大全網 - 新華字典 - json.loads()方法將壹個json串轉化為dict的時候,元素順序變化了

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'>。