主要使用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>执行输出如下:</span> jsonStr: {"info": {"code": 1, "sex": "u7537", "id": 1900, "name": "u5f20u4e09"}, "code": 1, "name": "u5f20u4e09", "sex": "u7537", "data": [{"code": 1, "sex": "u7537", "id": 1900, "name": "u5f20u4e09"}, {"code": 1, "sex": "u7537", "id": 1900, "name": "u5f20u4e09"}, {"code": 1, "sex": "u7537", "id": 1900, "name": "u5f20u4e09"}], "id": 1900}
以上就是小编为大家带来的python中json格式数据输出的简单实现方法全部内容了,希望大家多多支持查字典教程网~