我要访问 session_key 来自 JSON 响应。在登录 api 中,我将凭据发送回 Content-Type: application/json
我需要访问 session_key。如何访问 session_key 以获取 json 响应。
credential["username"]="john"
credential["password"]="xxx"
response =c.put('/api/login', data=json.dumps(credential))
JSON 响应
print response.content
输出
{"message": "", "result": {"username": "john", "session_key": "xyx"}}
当我尝试使用 print(response.content.result) 时出现错误
AttributeError: 'str' object has no attribute 'result'
最佳答案
如 response.content
是 json 你只需要将它解码为 python dict。
import json
content = json.loads(response.content)
key = content['result']['session_key']
关于json - 如何在 Django 中访问 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645190/