1.json报错
-
json中的load/loads不能反序列化多个json对象,只能反序列一个json对象
-
只能被load一次
-
json.decoder.JSONDecodeError: Extra data: line 1 column 40 (char 39)
import json
goods=[{"id": 1, "title": "飞机", "price": 1000},
{"id": 3, "title": "大炮", "price": 1000},
{"id": 8, "title": "迫击炮", "price": 1000},
{"id": 9, "title": "手枪", "price": 1000}]
with open("test.txt","w",encoding="utf-8") as f:
data=json.dump(goods,f,ensure_ascii=False)
with open("test.txt","r",encoding="utf-8") as f:
data=json.load(f)
print(data)
#不报错,因为goods是一个json对象
#方式二:添加很多歌json对象在文件中
with open("test.txt","w",encoding="utf-8") as f:
for i in goods:
json.dump(i,f,ensure_ascii=False)
with open("test.txt","r",encoding="utf-8") as f:
data=json.load(f)
print(data)
#报错 文件中包含很多json对象字符串,不能一下子反序列化所有json对象,
#会报错,应该是反序列化过程中字符串不匹配导致
#也报错 ==>json中的load/loads不能反序列化多个json对象,只能反序列一个json对象
data=f.read()
data=json.loads(data)
print(data)
#json.decoder.JSONDecodeError: Extra data: line 1 column 40 (char 39)