python - 在 Python 中加载 JSON 并通过内部变量引用
问题描述
我有 JSON
[{"id": 1, "title": "ble", "description": "ble", "done": true}, {"id":2, "title": "2", "description": "2", "done": true}]
我想将它加载到 Python 3 字典中,然后我可以通过id
JSON 引用它,就像print(json[2])
打印一个项目一样。
解决方案
使用内置json
包:https ://docs.python.org/3/library/json.html
import json
# some JSON:
stringifiedJSON = '[{"id": 1, "title": "ble", "description": "ble", "done": true}, {"id":2, "title": "2", "description": "2", "done": true}]'
# parse x:
parsedJSON = json.loads(stringifiedJSON) # a dict representing the JSON
# access specific field
print(parsedJson[1])
输出:
>>> { "id":2, "title":"2", "description":"2", "done":true }
请注意,无法访问2
您提供的 JSON 字符串示例中的键,因为它是一个只有 2 个元素的数组(索引从零开始)。
推荐阅读
- java - 使用java删除Excel中的'0'和'n/a'
- java - 如何从左侧为 ImageView 设置动画以填充屏幕?
- java - 将字符串从java客户端发送到局域网中的c#服务器
- html - 301重定向单页导致链接地址错误
- jquery - 如何结合数据表信息文本和分页链接
- javascript - 如何循环遍历父数组的子数组但遍历每个子数组的一个索引
- android - 在android中使用jsoup时,我要连接的站点和加载的站点不同
- python - 如何在 Flask 中分别上传多个文件?
- c# - Visual Studio 与 Unity 不对应
- hadoop - 在 HBase 中如何读取数据?