从 ID JSON 响应 surveymonkey API v3 获取值

Get value from ID JSON response surveymonkey API v3

我对此很陌生,非常感谢任何指导或帮助。

我目前有一份通过以下电话获得的调查回复列表:

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?

中获取实际响应值,例如 "Yes, No, Maybe"

非常感谢, 庞

目前 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 本身允许一个选项来为您填写答案文本,那也不是一个坏主意。