遍历JSON请求Alexa技能

时间:2018-08-31 13:48:25

标签: python aws-lambda alexa alexa-skills-kit alexa-skill

我似乎在遍历Alexa技能中从URL提取的数据时遇到问题,这是我的示例代码:

def get_elevator_status():
session_attributes = {}
card_title = "Septa Elevator Status"
reprompt_text = ""
should_end_session = False


response = urllib2.urlopen(API_BASE_URL + "/elevator")
septa_elevator_status = json.load(response) 


for elevators in septa_elevator_status['results']:
    speech_output = "The following elevators are out of service." "On " + elevators['line'] + " at station " + elevators['station'] + " the " + elevators['elevator'] + " elevator has " + elevators['message']

如果我在python shell中运行代码并打印出结果(如果有多个中断),它将打印出所有行。但是,当我测试我的Alexa技能并要求中断时,即使有多次电梯中断,它也只会报告一个结果。我是否缺少某些东西可以工作?该代码应该循环遍历并说所有结果正确吗?

2 个答案:

答案 0 :(得分:0)

您可以发布“响应” json吗,我猜是json格式不正确

答案 1 :(得分:0)

每次您用特定的speech_output覆盖elevators时遍历结果集。相反,您必须将每个结果连接到speech_output

for elevators in septa_elevator_status['results']:
    speech_output =  speech_output  + " <s> The following elevators are out of service." "On " + elevators['line'] + " at station " + elevators['station'] + " the " + elevators['elevator'] + " elevator has " + elevators['message'] + "</s>"

<s>是句子标记,仅在使用SSML时才使用。