Please help me) I'm inquiring about how to call JSON on OrderedDict

Asked 2 years ago, Updated 2 years ago, 103 views

Since json.dump can write OrderDict, I know that OrderDict can be used as JSON's input.

What should I do if I want to write output?

json python load ordereddictionary

2022-09-22 22:12

1 Answers

This is possible by using the factor object_pairs_hook in JSONDecoder.

>>> json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode('{"foo":1, "bar": 2}')
OrderedDict([('foo', 1), ('bar', 2)])
>>> 

Now pass this parameter to json.loads

A similar approach can be used with json.load.

>>> data = json.load(open('config.json'), object_pairs_hook=OrderedDict)


2022-09-22 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.