我们在调用一些api的时候,经常对得到返回结果为json字符串格式的数据,但是在输出的时候,内容全是在一行
而我们如果只是使用json.dumps()方法的时候,类似于下面
最后的结果就会是这样
可以使用load方法来格式化保存这种数据 ,json_resp为json字符串格式的数据(全部都在一行)
json_load = json.loads(json_resp) save_jsonfile = r"test.json" with open(save_jsonfile, mode='w', encoding='utf-8') as json_file: json.dump(json_load, json_file, ensure_ascii=False, indent=2) print('jsonfile生成完成--')
最后的结果就会使格式化后的结果了