我尝试使用此行(请求库)POST请求从客户端获取JSON数据:
request.data
如何将其转换为dict?
有效:
response_data = request.get_json()
但是如何将此转换成dict?
答案 0 :(得分:2)
编辑:对于发布请求:
import requests
import json
url = "https://jsonplaceholder.typicode.com/posts/"
payload = {
"userId": 10,
"id": 901,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "c71c65a6-07f4-a2a4-a6f8-dca3fd706a7a"
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
print(type(response.json()))
class'dict'
您可以使用以下内容:
import requests
url = "https://api.icndb.com/jokes/random"
headers = {
'cache-control': "no-cache",
'postman-token': "77047c8b-caed-2b2c-ab33-dbddf52a7a9f"
}
response = requests.request("GET", url, headers=headers)
print(type(response.json()))
class'dict'
答案 1 :(得分:0)
// forward declaration
class Warrior;
class Noble;
class Warrior {
// ...
};
class Noble {
// ...
};
您需要将import json
response_data = json.loads(response.text)
result = response_data.get('result')
反序列化为dict,然后将用户response.text
与相应的密钥进行反序列化。 .get
是上述示例中的关键。并且result
是网址调用的响应。