一、定义
- json全称:javascript object notation
- 前后端交互的枢纽:后端通过将程序产生的字典转化成json格式的json字符串(串),然后通过网络传输,给到前端,前端解析json文件,完成数据交互
二、python字典与json字符串相互转换
1. python字典 ------> json字符串:dumps
- ensure_ascii:默认为True。如果ensure_ascii为False, 那么写入的字符串中可以包含非ASCII字符
import jsondic = {"name": "Tom", "age": 18, "hobby": "篮球", 1: False, 2: None}s = json.dumps(dic, ensure_ascii=False)print(s) ?# {"name": "Tom", "age": 18, "hobby": "篮球", "1": false, "2": null}
2. json字符串 ------> python字典:loads
s = ‘{"name": "Tom", "age": 18, "hobby": "篮球", "1": false, "2": null}‘dic = json.loads(s)print(dic) ??# {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘篮球‘, ‘1‘: False, ‘2‘: None}
三、读、写json文件
1. dump
将python字典转换成json字符串,并写入文件中
- indent:缩进
import jsondic = {"name": "Tom", "age": 18, "hobby": "篮球", 1: False, 2: None}f = open("1.json", "w", encoding="utf-8") ?# 把对象打散成json写入到文件中json.dump(dic, f, ensure_ascii=False, indent=4)f.close()
结果:
2. load
- 读取json文件,并解析成字典
import jsonf = open("1.json", encoding="utf-8")dic = json.load(f)f.close()print(dic) ?# {‘name‘: ‘Tom‘, ‘age‘: 18, ‘hobby‘: ‘篮球‘, ‘1‘: False, ‘2‘: None}
四、读、写多个json字符串
1. 问题
我们可以向同一文件中写入多个json字符串,但是json文件中内容是一行内容。此时读取时无法读取。
import jsonlst = [{"a": 1}, {"b": 2}, {"c": 3}]f = open("2.json", "w")for el in lst: ???json.dump(el, f)f.close()
结果:
f = open("2.json")dic = json.load(f)结果:json.decoder.JSONDecodeError: Extra data: line 1 column 9 (char 8)
2. 解决方案
改用dumps和loads,对每一行分别做处理
import jsonlst = [{"a": 1}, {"b": 2}, {"c": 3}]f = open("2.json", "w")for el in lst: ???s = json.dumps(el) + "\n" ???f.write(s)f.close()
结果:
f = open("2.json")for line in f: ???dic = json.loads(line.strip()) ???print(dic)f. close()
结果:
{‘a‘: 1}
{‘b‘: 2}
{‘c‘: 3}
五、json处理类
1. 将类转换成json字符串
import jsonclass Person(object): ???def __init__(self, name, age): ???????self.name = name ???????self.age = agep = Person("Tom", 18)def func(obj): ???return {"name": obj.name, "age": obj.age}s = json.dumps(p, default=func)print(s) ?# {"name": "Tom", "age": 18}
2. 将json字符串赋给类
class Person(object): ???def __init__(self, name, age): ???????self.name = name ???????self.age = ages = ‘{"name": "Tom", "age": 18}‘def func(dic): ???return Person(dic["name"], dic["age"])p = json.loads(s, object_hook=func)print(p.name, p.age) ?# Tom ?18
json
原文地址:https://www.cnblogs.com/ipython-201806/p/9964571.html