python json

json 模块

Javascript Object Notation(标记)

java脚本对象标记语言,将所有东西都变成字符串形式

python中的set 集合无法用json表示出来

线性数据:

流式数据 "磁盘上的文件" 一大堆0101010排列起来的

数据之间没有引用关系

image-20200609211054719

结构化数据:

内存中:

'abc'.upper() 字符串

image-20200609210944271

先把数据拆分出门 再把零件组装

serialization deserialization

序列化: 将内存中的数据转换成字节串,用以保存在文件或通过网络传输,称为序列化过程

反序列化 将文件中或者网络中获取的数据转换成内存中原来的数据类型称为反序列化的过程

image-20200609220617679

从内存中 到内存中叫dumps 从内存中到文件中叫dump

这里有个生动形象的例子:

with open("a.txt", mode="wt", encoding="utf-8") as f:
    f.write(10)
wt: write text
wb: write binary   # t和b 表示数据的单位
a: append 
w: write 覆盖写模式  # w和r 表示数据的方向 是读取还是写入
r: read
+: 加个
默认是 rt格式

如果这么写代码,会报错,显示wirte必须为str数据类型

也就意味着 如果我们没有字符串类型的数据,json也就用不了,没有意义.序列化的根本是字节,json是一种不彻底的序列化方式

Serialization_demo.py

s = json.dumps([1, 2, 3]) 把指定的对象转换为json格式的字符串
print(type(s))
print(s)  #  '[1, 2, 3]'
print(s.split(','))

(venv) C:\work\day16>python Serialization_demo.py
<class 'str'>
[1, 2, 3]
['[1', ' 2', ' 3]']

看上去好像没什么区别,但实际上是字符串
也就是 '[1, 2, 3]'

在强调一遍,并不是所有的数据类型都能转化为json的字符串

2. 元组

s = json.dumps((1, 2, 3))
print(s)

(venv) C:\work\day16>python Serialization_demo.py
[1, 2, 3]

json也能识别元组数据类型,将元组序列化后变成列表类型的字符串

将列表转换为元组

a = [1, 2, 3]
b = tuple(a)
print(type(a))

(venv) C:\work\day16>python Serialization_demo.py
<class 'list'>

3. 字典

s = json.dumps({'name': 'Andy', 'age': 10})
print(s)
print(type(s))

(venv) C:\work\day16>python Serialization_demo.py
{"name": "Andy", "age": 10}
<class 'str'>

4. 集合

s = json.dumps(set('abc'))

TypeError: Object of type set is not JSON serializable

(venv) C:\work\day16>
集合不可JSON序列化的类型

5.写入到文件dump

# 将json结果写到文件中
with open('a.txt', mode='at', encoding="utf-8") as f:
    json.dump([1, 2, 3], f)
    
    

image-20200610123935585

发现当前目录下自动写入了一个txt文件

2. 反序列化

2.1 List

# 反序列化
res = json.dumps([1, 2, 3])  # List序列化反序列化都是List
lst = json.loads(res)
print(type(lst))
print(lst)


a = '[1, 2, 3]'
b = str(a[1:])
c = str(b[:-1])
d = c.strip()
e = d.split(',')
print(e)

(venv) C:\work\day16>python Serialization_demo.py
<class 'list'>
[1, 2, 3]

2.2 元组

# 反序列化
res = json.dumps((1, 2, 3))
lst = json.loads(res)
print(type(lst))
print(lst)

(venv) C:\work\day16>python Serialization_demo.py
<class 'list'>
[1, 2, 3]

元组序列化后变成List样的字符串,反序列化后又变成List.无法回到元组类型

2.3 从文件中反序列化

从内存中到内存中 loads

从文件到内存 load


总结

# json
# json.dumps(obj)  # 内存到内存中
# json.dump(obj, f)  # 内存到文件中
# json.loads()  # 内存中有一个数据转为内存中
# json.load(f)  # 将f文件中的格式回复到原本的格式


# 把需要序列化的对象, 通过多次序列化的方式,用文件的write方法把多次序列化后的字符串写到文件中
with open('json.txt', mode='at', encoding='utf-8') as f:
    f.write(json.dumps([1, 2, 3]) + '\n')
    f.write(json.dumps([4, 5, 6]) + '\n')
    
# 为什么要加 '\n'
如果不加的话将来读取不回来了, 因为两个中括号在一起了

# 把分次序列化的json字符串, 反序列化回来
with open('json.txt', mode='rt', encoding='utf-8') as f:
    res = json.loads(f.readline().strip())
    res2 = json.loads(f.readline().strip())
    print(type(res))
    print(res)
    print(type(res2))
    print(res2)
    
做法二:
with open('json.txt', mode='rt', encoding='utf-8') as f:
    for x in f:
        print(json.loads(x.strip()))

image-20200610162058989

反序列化过程

image-20200610162212913

posted on 2020-07-05 21:26  sunnywillow  阅读(137)  评论(0编辑  收藏  举报