How to completely traverse a complex dictionary of unknown depth?

后端 未结 7 1968
深忆病人
深忆病人 2020-11-30 17:35

Importing from JSON can get very complex and nested structures. For example:

{u\'body\': [{u\'declarations\': [{u\'id\': {u\'name\': u\'i\',
            


        
7条回答
  •  孤城傲影
    2020-11-30 18:38

    Instead of writing your own parser, depending on the task, you could extend encoders and decoders from the standard library json module.

    I recommend this especially if you need to encode objects belonging to custom classes into the json. If you have to do some operation which could be done also on a string representation of the json, consider also iterating JSONEncoder().iterencode

    For both the reference is http://docs.python.org/2/library/json.html#encoders-and-decoders

提交回复
热议问题