python indent如何打印JSON数据

Python (185) 2023-04-29 09:05:38

说明

1、JSON是一种很好的序列格式,现在广泛应用于API和web服务,但是裸眼看大数据量的JSON很难,它们很长,还在一行。

2、JSON数据可以通过参数indent更好地打印,这在处理REPL或日志时非常有用。

实例

>>>importjson
>>>print(json.dumps(data))#Noindention
{"status":"OK","count":2,"results":[{"age":27,"name":"Oz","lactose_intolerant":true},{"age":29,"name":"Joe","lactose_intolerant":false}]}
>>>print(json.dumps(data,indent=2))#Withindention
{
"status":"OK",
"count":2,
"results":[
{
"age":27,
"name":"Oz",
"lactose_intolerant":true
},
{
"age":29,
"name":"Joe",
"lactose_intolerant":false
}
]
}

以上就是python indent打印JSON数据的方法,希望对大家有所帮助。

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

THE END

发表回复