Skip to main content
 首页 » 编程设计

python之从 python 中的 json 数组访问数据

2025年05月04日14dflying

我的一个回复看起来像,

{ 
    "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])