json 模块:处理 JSON 数据
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。
JSON 基础
JSON
的基础结构有两种:键值对 (name/value pairs
) 和数组 (array
)。
JSON
具有以下形式:
object
- 对象,用花括号表示,形式为(数据是无序的):{ pair_1, pair_2, …, pair_n }
pair
- 键值对,形式为:string : value
array
- 数组,用中括号表示,形式为(数据是有序的):[value_1, value_2, …, value_n ]
value
- 值,可以是string
字符串number
数字object
对象array
数组true / false / null
特殊值
string
字符串 例子:
- {
- "name": "echo",
- "age": 24,
- "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"],
- "ages for school": {
- "primary school": 6,
- "middle school": 9,
- "high school": 15,
- "university": 18
- },
- "hobby": ["sports", "reading"],
- "married": false
- }
JSON 与 Python 的转换
假设我们已经将上面这个 JSON
对象写入了一个字符串:
In [1]:
- import json
- from pprint import pprint
- info_string = """
- {
- "name": "echo",
- "age": 24,
- "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"],
- "ages for school": {
- "primary school": 6,
- "middle school": 9,
- "high school": 15,
- "university": 18
- },
- "hobby": ["sports", "reading"],
- "married": false
- }
- """
我们可以用 json.loads()
(load string) 方法从字符串中读取 JSON
数据:
In [2]:
- info = json.loads(info_string)
- pprint(info)
- {u'age': 24,
- u'ages for school': {u'high school': 15,
- u'middle school': 9,
- u'primary school': 6,
- u'university': 18},
- u'coding skills': [u'python',
- u'matlab',
- u'java',
- u'c',
- u'c++',
- u'ruby',
- u'scala'],
- u'hobby': [u'sports', u'reading'],
- u'married': False,
- u'name': u'echo'}
此时,我们将原来的 JSON
数据变成了一个 Python
对象,在我们的例子中这个对象是个字典(也可能是别的类型,比如列表):
In [3]:
- type(info)
Out[3]:
- dict
可以使用 json.dumps()
将一个 Python
对象变成 JSON
对象:
In [4]:
- info_json = json.dumps(info)
- print info_json
- {"name": "echo", "age": 24, "married": false, "ages for school": {"middle school": 9, "university": 18, "high school": 15, "primary school": 6}, "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"], "hobby": ["sports", "reading"]}
从中我们可以看到,生成的 JSON
字符串中,数组的元素顺序是不变的(始终是 ["python", "matlab", "java", "c", "c++", "ruby", "scala"]
),而对象的元素顺序是不确定的。
生成和读取 JSON 文件
与 pickle
类似,我们可以直接从文件中读取 JSON
数据,也可以将对象保存为 JSON
格式。
json.dump(obj, file)
将对象保存为 JSON 格式的文件json.load(file)
从 JSON 文件中读取数据
In [5]:
- with open("info.json", "w") as f:
- json.dump(info, f)
可以查看 info.json
的内容:
In [6]:
- with open("info.json") as f:
- print f.read()
- {"name": "echo", "age": 24, "married": false, "ages for school": {"middle school": 9, "university": 18, "high school": 15, "primary school": 6}, "coding skills": ["python", "matlab", "java", "c", "c++", "ruby", "scala"], "hobby": ["sports", "reading"]}
从文件中读取数据:
In [7]:
- with open("info.json") as f:
- info_from_file = json.load(f)
- pprint(info_from_file)
- {u'age': 24,
- u'ages for school': {u'high school': 15,
- u'middle school': 9,
- u'primary school': 6,
- u'university': 18},
- u'coding skills': [u'python',
- u'matlab',
- u'java',
- u'c',
- u'c++',
- u'ruby',
- u'scala'],
- u'hobby': [u'sports', u'reading'],
- u'married': False,
- u'name': u'echo'}
删除生成的文件:
In [8]:
- import os
- os.remove("info.json")