【问题标题】:Looping through an json request Alexa skill循环通过 json 请求 Alexa 技能
【发布时间】:2019-02-06 13:42:57
【问题描述】:

我似乎在遍历我从 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 技能并要求停机时,即使有多个电梯停机,它也只会报告一个结果。我错过了一些东西吗?这段代码是否应该循环并说出所有找到的结果?

【问题讨论】:

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


    【解决方案1】:

    你能贴出'response' json,我猜是json格式不正确

    【讨论】:

      【解决方案2】:

      在每次使用特定的elevators 覆盖speech_output 时循环遍历结果集。相反,您必须将每个结果连接到 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>"
      

      &lt;s&gt; 是句子标签,只有在使用 SSML 时才使用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 2018-09-24
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多