1.什么是json數(shù)據(jù)
- json是一種數(shù)據(jù)格式杆煞,滿足json格式的數(shù)據(jù)就是json數(shù)據(jù)故硅。
文件后綴是.json,并且文件中內(nèi)容滿足json格式
2.json格式
- 一個json中只有一個數(shù)據(jù)爹橱;并且這個數(shù)據(jù)是json支持的數(shù)據(jù)類型的數(shù)據(jù)
json支持的數(shù)據(jù)類型
數(shù)字類型 - 包含所有的數(shù)字萨螺,包括整數(shù)和小數(shù), 例如: 100, 12.5愧驱, -20
字符串 - 使用雙引號括起來字符集, 例如: "123", "abc123", "&*ash"
布爾 - true和false
數(shù)組 - 相當于python中的列表, 使用中括號括起來慰技,括號里面是json支持的任意類型的數(shù)據(jù)
例如:["abc", 100, true], [12, 89, 89, 90]
字典 - 相當于python中的字典, 使用{}括起來,括號里面是鍵值對组砚。
鍵一般是字符串吻商,值是json支持的任意類型的數(shù)據(jù)
{"name": "張三", "age": 18}
特殊值 - null(相當于None), 表示空
3.python中有一個內(nèi)置的模塊用來支持對json數(shù)據(jù)的處理: json
a.將json數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)
b.將python數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù)
import json
1.將json數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)
- loads(字符串) - 將json格式的數(shù)據(jù)轉(zhuǎn)換python對應的數(shù)據(jù)
注意:這兒的字符串的內(nèi)容必須是json格式的數(shù)據(jù)
json python
數(shù)字 整型/浮點型
字符串 字符串(雙引號會變單引號)
布爾 布爾(true -> True, false -> False)
數(shù)組 列表
字典 字典
null None
# a. 數(shù)字轉(zhuǎn)int/float
py1 = json.loads('100')
print(py1, type(py1))
py2 = json.loads('100.12')
print(py2, type(py2))
# b. 字符串
py3 = json.loads('"json"')
print(py3, type(py3))
# c. 布爾
py4 = json.loads('true')
print(py4)
# d.列表
py5 = json.loads('[100, "abc", true, null]')
print(py5)
# e.字典
py6 = json.loads('{"a": 1, "b":[1, 2], "c": true}')
print(py6)
# 練習1:
# 讀文件中的內(nèi)容
with open('data.json', encoding='utf-8') as f:
content = f.read()
# 將讀出來的內(nèi)容轉(zhuǎn)換成python數(shù)據(jù)
data_dict = json.loads(content)
print(data_dict['data'][2]['age'])
2.將python數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù)
dumps(數(shù)據(jù)) - 將python數(shù)據(jù)轉(zhuǎn)換成內(nèi)容符合json格式的字符串
注意:最終結(jié)果是字符串
python json
int/float 數(shù)字
字符串 字符串(單引號會變雙引號)
布爾 布爾(True - true, False - false)
列表/元祖 數(shù)組
字典 字典
# a. int和float
js1 = json.dumps(100)
print(js1, type(js1))
js1 = json.dumps(100.12)
print(js1, type(js1))
# b.字符串
js2 = json.dumps('hello world')
print(js2)
# c.布爾
js3 = json.dumps(True)
print(js3)
# d.列表糟红、元祖
js4 = json.dumps((10, 'abc', True))
print(js4)
js4 = json.dumps([100, 'aaa', False, None])
print(js4)
# e.字典
js5 = json.dumps({'a': 10, 'b': 'abc', 'c': ['a', 'b']})
print(js5)
# 練習2:添加多個學生信息(姓名艾帐,年齡,電話)盆偿,添加完成后柒爸,將數(shù)據(jù)保存到j(luò)son文件中
# 并且,上次添加的信息不會刪除事扭,下次再添加的時候捎稚,是在上次的基礎(chǔ)上添加的
"""
姓名:
年齡:
電話:
aaa, 12, 12222
"""
# all_students = [] # 保存所有學生的信息
with open('students.json', encoding='utf-8') as f:
# 從文件中把數(shù)讀出來
content = f.read()
# 將數(shù)據(jù)轉(zhuǎn)換成列表
all_students = json.loads(content)
# while True:
# name = input('姓名:')
# age = int(input('年齡:'))
# tel = input('電話:')
# # 創(chuàng)建學生對應的字典
# stu = {'name': name, 'age': age, 'tel': tel}
# # 保存學生信息
# all_students.append(stu)
#
# value = input('是否繼續(xù)(Y/N):')
# if value == 'N':
# break
print(all_students)
# 將數(shù)據(jù)寫入文件中
with open('students.json', 'w', encoding='utf-8') as f:
content = json.dumps(all_students)
f.write(content)
3.json文件操作相關(guān)方法
- load(文件對象) - 將文件對象中的數(shù)據(jù)讀出來,并且轉(zhuǎn)換成python對應的數(shù)據(jù)
(文件對象中的內(nèi)容必須是json格式的數(shù)據(jù))
dump(數(shù)據(jù), 文件對象) - 將python數(shù)據(jù)轉(zhuǎn)換成json格式的字符串求橄,再寫入文件對象中
print('================')
with open('test.txt', encoding='utf-8') as f:
content = json.load(f)
print(content, type(content), sep='\n')
# with open('new.json', 'w', encoding='utf-8') as f:
# json.dump([1, 2, 3], f)
def yt_dump(obj, file):
with open(file, 'w', encoding='utf-8') as f:
strstr = json.dumps(obj)
f.write(strstr)
yt_dump(['a', 'b', 'c'], 'new.json')