python json按输入顺序输出内容

时间:2022-01-28 07:58:23
通过使用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)

结果:

dic is: odict_items([('a', 1), ('b', 2), ('c', 3)])
jsons: {"a": 1, "b": 2, "c": 3}