python - 根据项目类型在python中解析json嵌套的dict项目
问题描述
我想仅根据存储在名为 result 的变量中的本地 json 文件中的键“type”中的“Username”值列出“name”键中的值(使用 json.load(input_file)。文件包含我不希望在结果中出现的其他非用户名字典。
{
"count": {
"references": {
"returned": 10,
"total": 17933
}
},
"entities": {
"KF4UXbl": {
"name": "johndoe123",
"type": "Username",
},
"KeirTr": {
"name": "#thash123",
"hits": 141581,
"type": "Hashtag",
},
"SQsD6o": {
"name": "goodie1234",
"type": "Username",
"modified": "2019-04-12T21:02:51.938Z"
},
"type:Speech": {
"name": "Other",
"type": "MetaType",
},
"QXyWAK": {
"name": "textuser1234",
"type": "Username",
},
"type:Topic": {
"name": "Topic",
"type": "MetaType",
},
"KeTJkZ": {
"name": "TestOrg",
"type": "Organization",
"meta_type": "type:Organization",
},
}
代码
for i, j in result.iteritems():
if j.get('type') == 'Username':
print j.get('name')
但是我收到此错误:
if j.get('type') == 'Username':
AttributeError: 'list' object has no attribute 'get'
如何根据用户名的值打印以下内容?
johndoe123
goodie1234
textuser1234
解决方案
推荐阅读
- php - 在php中以特定格式显示月份数组中的日期
- html - 圆圈问题之外的圆形图标
- jenkins - 如果 repo 已经克隆,如何在 Jenkinsfile 中编写条件,跳过该阶段
- python - 我有三个列表,我想创建一个 csv 文件
- android - 使用 beginTransaction() 从一个 Fragment 到另一个 Fragment 的事务
- javascript - 清单错误:页面无法脱机工作
- java - 使用java以秒为单位查找日期之间的差异
- ios - Firestore 基于角色的规则
- c++ - 如何比较两个向量的值
- python - 如何为 tkinter 安装字体