Can I get JSON to load into an OrderedDict?

前端 未结 6 938
别跟我提以往
别跟我提以往 2020-11-22 01:13

Ok so I can use an OrderedDict in json.dump. That is, an OrderedDict can be used as an input to JSON.

But can it be used as an output? If so how? In my

6条回答
  •  温柔的废话
    2020-11-22 01:55

    The normally used load command will work if you specify the object_pairs_hook parameter:

    import json
    from  collections import OrderedDict
    with open('foo.json', 'r') as fp:
        metrics_types = json.load(fp, object_pairs_hook=OrderedDict)
    

提交回复
热议问题