主要使用json模块,直接导入import json即可。
小例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#coding=UTF-8
import json
info = {}
info[ "code" ] = 1
info[ "id" ] = 1900
info[ "name" ] = '张三'
info[ "sex" ] = '男'
list = [info,info,info]
data = {}
data[ "code" ] = 1
data[ "id" ] = 1900
data[ "name" ] = '张三'
data[ "sex" ] = '男'
data[ "info" ] = info
data[ "data" ] = list
jsonStr = json.dumps(data)
print "jsonStr:" ,jsonStr
|
1
2
3
|
关键代码:jsonStr = json.dumps(data)
<span style = "font-family:Arial;BACKGROUND-COLOR: #ffffff" >执行输出如下:< / span>
jsonStr: { "info" : { "code" : 1 , "sex" : "\u7537" , "id" : 1900 , "name" : "\u5f20\u4e09" }, "code" : 1 , "name" : "\u5f20\u4e09" , "sex" : "\u7537" , "data" : [{ "code" : 1 , "sex" : "\u7537" , "id" : 1900 , "name" : "\u5f20\u4e09" }, { "code" : 1 , "sex" : "\u7537" , "id" : 1900 , "name" : "\u5f20\u4e09" }, { "code" : 1 , "sex" : "\u7537" , "id" : 1900 , "name" : "\u5f20\u4e09" }], "id" : 1900 }
|
以上就是小编为大家带来的python中json格式数据输出的简单实现方法全部内容了,希望大家多多支持服务器之家~