JSON for python

来源:互联网 发布:下载软件苹果助手 编辑:程序博客网 时间:2024/04/25 21:13

了解 json

JSON介绍
JSON官方说明
Python操作json的标准api库

练习 json

#!/usr/bin/env python#coding:utf-8"""FuncName: json.pyDesc: study json for python 2.7Date: 2016-09-06 10:30Author: johnny"""import jsondata = {    'name' : 'Johnny',    'home' : 'http://blog.csdn.net/z_johnny',    'date' : 20160906}json_str = json.dumps(data)         # 字符串编码JSON数据,<type 'str'>print type(json_str)print repr(data),type(repr(data))   # 原始输出,repr(data),<type 'str'>python_dict = json.loads(json_str)  # 字符串解码JSON数据,<type 'dict'>print type(python_dict)with open('jsonfile.json', 'w') as f:   # 文件编码JSON数据,写JSON数据    json.dump(data, f)with open('jsonfile.json', 'r') as f:   # 文件解码JSON数据,读JSON数据,<type 'dict'>    data_dict = json.load(f)    print type(data_dict)#带参数,example:ex = json.dumps(data,sort_keys=True,indent=4)   # sort_keys 排序,indent 缩进print ex"""{    "date": 20160906,    "home": "http://blog.csdn.net/z_johnny",    "name": "Johnny"}"""

python原始类型向json类型的转化表:
这里写图片描述

json类型到python类型转化表:
这里写图片描述

0 0
原创粉丝点击