json库报错(TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper)

使用json库导入json文件时,报错

TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

import json
f = open("data/data.json","r")
data = json.loads(f)
print(data)


报错原因是loads方法去将json文件转换为python对象

json有四个方法:dumps、loads、dump和load。dumps和loads是在内存中转换(python对象和json字符串之间的转换),而dump和load则是对应于文件的处理。

修改为:


import json

f = open("data/data.json","r")
str = f.read()
data = json.loads(str)
print(data)


即可成功运行

 

你可能感兴趣的:(json,开发语言,python)