Files
ailearning/docs/da/142.md
2020-10-19 21:08:55 +08:00

204 lines
4.4 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# json 模块:处理 JSON 数据
[JSON (JavaScript Object Notation)](http://json.org) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。
## 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` 字符串
例子:
```py
{
"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]:
```py
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]:
```py
info = json.loads(info_string)
pprint(info)
```
```py
{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]:
```py
type(info)
```
Out[3]:
```py
dict
```
可以使用 `json.dumps()` 将一个 `Python` 对象变成 `JSON` 对象:
In [4]:
```py
info_json = json.dumps(info)
print info_json
```
```py
{"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]:
```py
with open("info.json", "w") as f:
json.dump(info, f)
```
可以查看 `info.json` 的内容:
In [6]:
```py
with open("info.json") as f:
print f.read()
```
```py
{"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]:
```py
with open("info.json") as f:
info_from_file = json.load(f)
pprint(info_from_file)
```
```py
{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]:
```py
import os
os.remove("info.json")
```