SoFunction
Updated on 2025-03-01

python json load json solution for out-of-order after data

As we all know:python json string that can be converted, but when converting it to a dictionary, there is a disordered order

The dictionary is a hash structure, that is, it sorts according to the key, and the order cannot be guaranteed.

import json

jsonstr = '{"username":"string","age":"int","income":"float","createdTime":"date"}'

print((jsonstr))

The output results are inconsistent

Code Print

{'age': 'int', 'createdTime': 'date', 'username': 'string', 'income': 'float'}

Console:

>>> import json
>>> jsonstr = '{"username":"string","age":"int","income":"float","createdTime":"date"}'
>>> print((jsonstr))
{'username': 'string', 'age': 'int', 'income': 'float', 'createdTime': 'date'}
>>>

Finally, an ordered dictionary collection is used: , the outputs of the two are consistent

dictStr = (jsonstr,object_pairs_hook=)

Supplementary expansion: The difference between detailed counting and loads

Python neutralization implements "deserialization", the difference is:

loads targets memory objects, that is, serializes Python built-in data into strings

Use serialized objectsd_json=({'a':1, 'b':2}), here d_json is a wordstring '{"b": 2, "a": 1}'

d=(d_json) #{ b": 2, "a": 1}, re-deserialize to dict using load

load for file handles

If there is a json file locally, you cand=(open(''))

Correspondingly, dump is to serialize the built-in type into a json object and write it to a file.

The above solution to the out of order after the python json load json data is all the content I share with you. I hope you can give you a reference and I hope you support me more.