且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从ID JSON响应Surveymonkey API v3获得价值

更新时间:2021-12-18 04:42:15

目前,从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.