經過使用collecions,進行排序。collections是一個python的內建模塊。
import collections dic = collections.OrderedDict() # dic = dict() dic['a'] = 1 dic['b'] = 2 dic['c'] = 3 print("dic is:",dic.items()) import json jsons = json.dumps(dic) print("jsons:",jsons)
結果:python
dic is: odict_items([('a', 1), ('b', 2), ('c', 3)]) jsons: {"a": 1, "b": 2, "c": 3}