python - 如何获取 json 对象中的键值(Python)
问题描述
这是我的 json :
{'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}
我想获取名称的所有值的列表。
我在 python 中的代码应该是什么
解决方案
d.values
给出所有的值,然后你可以得到name
每个值的属性。
d = {'1': {'name': 'poulami', 'password': 'paul123', 'profession': 'user', 'uid': 'poulamipaul'}, '2': {'name': 'test', 'password': 'testing', 'profession': 'tester', 'uid': 'jarvistester'}}
[i['name'] for i in d.values()]
['poulami', 'test']
另请注意,d.values
返回生成器而不是列表,以便转换为列表使用list(d.values())
推荐阅读
- android - INNER JOIN 造成内存问题
- android - AndroidStudio 4.0 Beta 1 中无法使用 FragmentContainerView 类
- macos - 通过 iTerm2 使用 Powerlevel10k 更改 Visual Studio 代码中的终端
- visual-studio - Visual Studio 正在创建 ref 文件夹
- c - C Socket 服务器未捕获传入连接
- swift - 将项目移动到部分
- nginx - 如何在 goaccess 中使用“HTTP 身份验证”
- html - 如何在角度变量中使用 =
- mysql - “utf-8”编解码器无法解码位置 3 中的字节 0xe6:无效的继续字节
- sql - 访问:如何查询直到找到最终父级?