我是一个新手,任何指导或帮助都是非常感谢的。
我目前有一份从以下电话中得到的调查回复列表:
import requests
import json
client = requests.session()
headers = {
"Authorization": "bearer %s" % "AccessTOKEN",
"Content-Type": "application/json"
}
HOST = "https://api.surveymonkey.net"
SURVEY_LIST_ENDPOINT = "/v3/surveys/SURVEYID/responses/bulk"
uri = "%s%s" % (HOST, SURVEY_LIST_ENDPOINT)
response = client.get(uri, headers=headers)
response_json = response.json()
print(response_json['data'])我得到的响应是以下内容:
{
"per_page": 50,
"total": 5114,
"data": [
{
"total_time": 40,
"href": "https://api.surveymonkey.net/v3/surveys/surveyID/responses/ID",
"custom_variables": {},
"ip_address": "IP ADDRESS",
"id": "ID",
"logic_path": {},
"date_modified": "2015-12-01T05:31:22+00:00",
"response_status": "completed",
"custom_value": "",
"analyze_url": "http://www.surveymonkey.com/analyze/browse/ID?respondent_id=ID",
"pages": [
{
"id": "220527570",
"questions": [
{
"id": "872991507",
"answers": [
{
"choice_id": "9573882449",
"row_id": "9573882439"
}
]我想从choice_id得到实际的响应值,例如“是,不,也许”?
非常感谢你,Pon
发布于 2017-06-26 22:15:31
目前还没有直接从API返回的有效负载包含答案文本。
你需要先拿到你的survey details:
SURVEY_DETAIL_ENDPOINT = "/v3/surveys/SURVEYID/details"
uri = "%s%s" % (HOST, SURVEY_DETAIL_ENDPOINT)
response = client.get(uri, headers=headers)
survey_data = response.json()然后,您可能希望遍历答案以创建查找字典。大致是这样的:
answer_dict = {}
for page in survey_data['pages']:
for question in page['questions']:
# Rows, choices, other, and cols all make up the possible answers
answers = question['answers'].get('rows', [])\
+ question['answers'].get('choices', [])\
+ question['answers'].get('other', [])\
+ question['answers'].get('cols', [])
for answer in answers:
answer_dict[answer['id']] = answer我知道这在for循环中看起来很粗糙,但基本上你只是遍历了整个调查。这是因为选项ID是全局唯一的(即列、行、选项等之间没有冲突)。
然后,您可以轻松地通过对响应中的任何answer执行answer_dict[answer['choice_id']]来获取完整的答案详细信息。
如果API本身允许一个选项来为您填写答案文本,这将是一个不错的主意。
https://stackoverflow.com/questions/44759124
复制相似问题