python - json.load 和 json.loads 无法加载 JSON 文件数据,Python
问题描述
请查看以下 JSON 文件,我一直在尝试阅读:
[
{
"age": "['3']",
"description": " Depending on the last time your little one was fed ",
"schedule": "Milk Feed",
"time": "6:15 am"
},
{
"age": "['3']",
"description": " Someone's feeling a bit sleepy!",
"schedule": "Naptime",
"time": "8:15 am - 9:15 am"
}
][
{
"age": "['4']",
"description": " Depending on the last time your little one was fed ",
"schedule": "Milk Feed",
"time": "6:15 am"
},
{
"age": "['4']",
"description": " Someone's feeling a bit sleepy!",
"schedule": "Naptime",
"time": "8:15 am - 9:15 am"
}
]
我已经尝试过JSON.load
, 和JSON.loads
用 json.load
AttributeError: 'str' object has no attribute 'read'
并使用 json.loads,我收到以下输出
raise JSONDecodeError("Expecting value", s, err.value) from None
这是我的代码
for line in open('baby_schedule.json', 'r'):
print(json.loads(line))
我需要你的帮助来加载这个 JSON,然后读取其中的单个项目。
解决方案
您的 json 无效。
这是正确的版本。
您应该使用json.loads
缓冲区和json.load
文件
[
{
"age": "['3']",
"description": " Depending on the last time your little one was fed ",
"schedule": "Milk Feed",
"time": "6:15 am"
},
{
"age": "['3']",
"description": " Someone's feeling a bit sleepy!",
"schedule": "Naptime",
"time": "8:15 am - 9:15 am"
},
{
"age": "['4']",
"description": " Depending on the last time your little one was fed ",
"schedule": "Milk Feed",
"time": "6:15 am"
},
{
"age": "['4']",
"description": " Someone's feeling a bit sleepy!",
"schedule": "Naptime",
"time": "8:15 am - 9:15 am"
}
]
推荐阅读
- python - PYTHON:DICT 的 IF 语句
- oracle - 在 Oracle 12.2c 中清除队列不更新计数器 un gv$aq
- python - 如何使用 html 中表单标签中的表单数据提取信息
- javascript - 如何将未使用的数据背景添加到图表js中的条形图
- arrays - 如何在查询返回空单元格的地方返回“空”谷歌表格
- c++ - 强制 C++ 类具有对齐的属性
- python - 如何让每辆车在 Or Tools 中做点什么?
- sql - Postgres:使用子查询更新表列
- python - 使用 django 框架加载 JSON 内容
- swift - 如何在以下 mvvm 架构中使用 @Binding Wrapper?