python中json格式数据输出的简单实现方法

主要使用json模块,直接导入import json即可。

小例子如下:

#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
关键代码: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}

相关推荐