python - 从python中的json数组访问数据
问题描述
我的一个回答看起来像,
{
"password": [
"Ensure that this field has atleast 5 and atmost 50 characters"
]
}
我正在尝试获取密码中的字符串。我怎么能得到它。
下面是我正在尝试的代码
key = json.loads(response['password'])
print(key[0]),
但它说'字符串索引必须是整数,而不是 str'
解决方案
错误信息是正确的。
key = json.loads(response['password'])
print(key[0]),
的格式json
是字符串。您需要将 json 对象的字符串转换为 python dict 才能访问它。
即:loads(string)
之前info[key]
key = json.loads(response)['password']
print(key[0])
推荐阅读
- reactjs - 反应路由器使用历史
- rust - 如何满足`impl futures::Future:futures::TryStream`的特征界限
- python - 使用 Cloudflare 背后的 API,使用 HTTP Post 和 Requests 库
- mysql - 使用联接时为每个不同的字段值选择随机行
- c - 为什么我使用 gotoxy(x,y) 时没有打印第一列
- jupyter-notebook - 使用 Jupyter Notebook 在降价文本中替换占位符(变量)的最简单方法?
- database - Sqlite 结构
- java - Hadoop Jar FileSystemNotFoundException
- linux - nano中的Ctrl+Z、Ctrl+X和Ctrl+C有什么区别?
- c++ - 不能将 CRTP 用于内部类