从ID JSON响应Surveymonkey API v3获得价值 [英] Get value from ID JSON response surveymonkey API v3

查看:66
本文介绍了从ID JSON响应Surveymonkey API v3获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im quite new to this, any guidance or help is much appreciated.

我目前有一个调查问卷清单,可通过以下电话获得:

I currently have a list of survey response i get form the following call:

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中获取实际的响应值,例如是,否,也许"?

I would like to get the actually response value e.g "Yes, No, Maybe" from the choice_id?

非常感谢, 庞

推荐答案

目前,从API返回的直接有效负载中也没有答案文本.

There isn't a direct payload returned from the API right now that has the answer text as well.

您需要先获取调查详细信息 :

SURVEY_DETAIL_ENDPOINT = "/v3/surveys/SURVEYID/details"
uri = "%s%s" % (HOST, SURVEY_DETAIL_ENDPOINT)

response = client.get(uri, headers=headers)

survey_data = response.json()

然后,您可能想遍历答案以创建查找字典.大致类似:

Then you likely want to loop through the answers to create a lookup dictionary. Roughly something like:

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在全局上是唯一的(即列,行,选择等之间没有冲突).

I know this looks rough with for loops but you're basically just traversing the entire survey. This works because choice IDs are globally unique (i.e. no collisions between columns, rows, choices etc.).

然后,您可以通过对响应中的任何answer进行answer_dict[answer['choice_id']]轻松获得整个答案的详细信息.

Then you can easily get the entire answer details by doing answer_dict[answer['choice_id']] for any answer in the response.

如果API本身允许您使用选项为您的答案填写答案文本,那将不是一个坏主意.

It wouldn't be a bad idea if the API itself allowed an option to fill in the answer text with the response for you.

这篇关于从ID JSON响应Surveymonkey API v3获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆