首页 > 解决方案 > 从python中的json数组访问数据

问题描述

我的一个回答看起来像,

{
    "password": [
        "Ensure that this field has atleast 5 and atmost 50 characters"
    ]
}

我正在尝试获取密码中的字符串。我怎么能得到它。

下面是我正在尝试的代码

key = json.loads(response['password'])
print(key[0]),

但它说'字符串索引必须是整数,而不是 str'

标签: pythonjson

解决方案


错误信息是正确的。

key = json.loads(response['password'])
print(key[0]),

的格式json是字符串。您需要将 json 对象的字符串转换为 python dict 才能访问它。

即:loads(string)之前info[key]

key = json.loads(response)['password']
print(key[0])

推荐阅读