Json
1. Json基础
- JSON的布尔值
- {“flag”:true}
- {“flag”:false}
- JSON的null
- {“name”:null}
- JSON对象
- json对象是用大括号{}围起来的
- {“name”:“tom”, “age”:20}
- JSON数组
- json数组用中括号[]围起来
- [
{“name”:“tom”, “age”:20},
{“name”:“jerry”, “age”:21},
{“name”:“dog”, “age”: 19}
]
- json文件
- json的文件类型是 .json
- json的MIME类型是application/json
2. Python类型与Json类型的转换
2.1 Python类型转Json类型的规则
Python | Json |
---|---|
dict字典 | object对象 |
list列表、tuple元组 | array数组 |
str | string |
True | true |
False | false |
None | null |
2.2 Json类型转Python类型的规则
Json | Python |
---|---|
object对象 | dict字典 |
array数组 | list列表 |
string | str |
true | True |
false | False |
null | None |
2.3 Python字典与Json对象的互转
- python的json模块可以对json进行解码编码
- 字典转JSON对象,使用json.dumps(dict_data)
import json
dict_data = {
'a':1,
'b':2,
'c':3,
'd':None,
'e':True,
'f':False,
'g':('xxx', 'yyy', 'zzz'),
'h': ['mmm', 'nnn', 'ppp']
}
print(type(dict_data)) # <class 'dict'>
# python字典转Json对象
json_data = json.dumps(dict_data)
print(type(json_data)) # 打印出来的是<class 'str'>
print(json_data) # 位置1
# json对象转python字典
dict_new = json.loads(json_data)
print(type(dict_new)) # <class 'dict'>
print(dict_new) # 位置2
- 位置1打印的如下
- 字典中的None已经转成了json的null
- 字典中的True、False已经转成了Json中的true和false
- 字典中元组和列表,已经转成了Json中的数组
{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”],
“h”: [“mmm”, “nnn”, “ppp”]
}
- 位置2打印的是
- 可以看到,此时json中的数组g,没法转回元组了,只能转成列表
{
‘a’:1,
‘b’:2,
‘c’:3,
‘d’:None,
‘e’:True,
‘f’:False,
‘g’:[‘xxx’, ‘yyy’, ‘zzz’],
‘h’: [‘mmm’, ‘nnn’, ‘ppp’]
}
2.4 Python列表与Json数组的互转
- 使用json.dumps(list_data),把python列表转成Json数组
- 使用json.loads(json_data),把json转成python的列表
import json
list_data = [
{'a': 1, 'b': 2, 'c': 3},
{'name': 'tom', 'xx': 2, 'yy': 33}
]
print(type(list_data)) # <class 'list'>
# python的列表转json
json_data=json.dumps(list_data)
print(type(json_data)) # <class 'str'>
print(json_data) # [{"a": 1, "b": 2, "c": 3}, {"name": "tom", "xx": 2, "yy": 33}]
# json转成python的列表
list_new = json.loads(json_data)
print(type(list_new)) # <class 'list'>
print(list_new) # [{'a': 1, 'b': 2, 'c':3}, {'name': 'tom', 'xx': 2, 'yy': 33}]
3. 处理Json文件
- dumps()和loads()用于处理json字符串
- dump()和load()用于处理json文件
3.1 从Json文件读取Json对象
a.json是一个json文件,里面是一个json对象
{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”]
}
- 从Json文件中读取Json对象,用data = json.load(f)
import json
# 读取Json文件中的Json对象
with open('a.json', 'r') as f:
data = json.load(f)
print(type(data)) # 打印<class 'dict'>
print(data) # 位置1
print(type(data['g'])) # 打印<class 'list'>
- 位置1打印的是
{
‘a’:1,
‘b’:2,
‘c’:3,
‘d’:None,
‘e’:True,
‘f’:False,
‘g’:[‘xxx’, ‘yyy’, ‘zzz’]
}
- 读进来的data已经是字典类型了,可以看到data[‘g’]是列表类型
- a.json文件里的null,读进来的是None
- a.json文件里的true、false,读进来的是True、False
3.2 从Json文件读取Json数组
b.json文件里的是一个Json数组
[
{“a”:1,“b”:2, “c”:3},
{“d”:4,“e”:5,“f”:6}
]
- 从Json文件中读取Json数组,用data = json.load(f)
import json
# 读取Json文件中的Json数组
with open('b.json', 'r') as f:
data = json.load(f)
print(type(data)) # <class 'list'>
print(data) # [{'a':1, 'b':2, 'c':3}, {'d':4, 'e':5, 'f':6}]
3.3 往文件里写入Json对象
- 往文件里写入json对象,用json.dump(data, f)
import json
# 直接在python中的字典里,写null、true、false会报错,得写成None、True、False
data = {
"a":1,
"b":2,
"c":3,
"d":None,
"e":True,
"f":False,
"g":("xxx", "yyy", "zzz")
}
# 将json对象写入json文件
with open('b.json', 'w') as f:
json.dump(data, f)
- 运行后,b.json文件中是
{
“a”:1,
“b”:2,
“c”:3,
“d”:null,
“e”:true,
“f”:false,
“g”:[“xxx”, “yyy”, “zzz”]
}
- 此时,python字典里的None、True、False,都已经转成了b.json文件里的null、true、false
3.4 往文件里写入Json数组
import json
data = [
{'a':1, 'b':2, 'c':3},
{'d':4, 'e':5, 'f':6}
]
# 把Json数组写入Json文件
with open('b.json', 'w') as f:
json.dump(data, f)
- 此时b.json文件里的数据是
[
{“a”:1, “b”:2, “c”:3},
{“d”:4, “e”:5, “f”:6}
]