JSON dumps custom formatting

后端 未结 3 1674
情深已故
情深已故 2020-12-03 16:56

I\'d like to dump a Python dictionary into a JSON file with a particular custom format. For example, the following dictionary my_dict,

\'text_li         


        
3条回答
  •  鱼传尺愫
    2020-12-03 17:32

    You will need to create a subclass of the json.JSONEncoder class and override the methods for each type of value so they write the format you need. You may end up re-implementing most of them, depending on what your formatting needs are.

    http://docs.python.org/2/library/json.html has an example for extending the JSONEncoder.

提交回复
热议问题