使用 indent 参数来定义缩进量

  1. import json
  2.  
  3. x = {
  4. "name": "Bill",
  5. "age": 63,
  6. "married": True,
  7. "divorced": False,
  8. "children": ("Jennifer","Rory","Phoebe"),
  9. "pets": None,
  10. "cars": [
  11. {"model": "Porsche", "mpg": 38.2},
  12. {"model": "BMW M5", "mpg": 26.9}
  13. ]
  14. }
  15.  
  16. # use four indents to make it easier to read the result:
  17. print(json.dumps(x, indent=4))