python - 如何使用 Python 访问 JSON 子数据
问题描述
我正在使用 Python 处理一些 JSON 数据。基本上我有以下 JSON 数据
{
"users": [
{
"name": "John Doe",
"prefix": "[Test Account]",
"rank": "10"
},
{
"name": "Jane Doe",
"prefix": "[Test Account]",
"rank": "10"
}
]
}
我有一个循环成功地遍历 Python 中的用户。
for x in data["users"]:
if username == data["users"]["name"]:
print("true")
我只想获得name
价值。在 JS 中你会这样做
data.users[x].name
但是你如何在 Python 中做到这一点。我试过像
data["users"][x]["name"]
和
data["users"[x]]["name"]
两者都抛出错误。任何帮助是极大的赞赏。
解决方案
尝试这个:
data = {
"users": [
{
"name": "John Doe",
"prefix": "[Test Account]",
"rank": "10"
},
{
"name": "Jane Doe",
"prefix": "[Test Account]",
"rank": "10"
}
]
}
username = "John Doe"
for x in data["users"]:
if username == x["name"]:
print("true")
推荐阅读
- rust - 这种理解是否正确:特征和函数重载都实现了临时多态性,但方向不同
- javascript - 为什么我的位置固定后背景图像消失了?
- python - 在不涉及数据帧的情况下按升序对条形图进行排序
- bash - 用于比较数组变量的 Bash If 语句
- haskell - 无论输入如何,程序都会不断进入边缘条件 - Haskell
- node.js - 在路由器中获取 req.param 未定义
- sharepoint - 如何将 > 100 万个项目加载到 SharePoint Online 列表
- python - 如何使用 BeautifulSoup 使用 txt 文件中的多个 URL
- sql - 我在 SQL 中遇到错误,无法修复此外键错误。有关详细信息,请参阅描述
- html - HTML:段落文本不堆叠成行。(所有的文字都在一行中并且在段落之外)