javascript - Dialogflow html/js 卡 json 值 v1

标签 javascript html json dialogflow-es

我正在尝试访问卡 json 值,但无济于事。

在我的场景中,我向机器人询问“伦敦的天气”,它回复“伦敦目前气温为 9 摄氏度”。通过网络钩子(Hook)。

这是正确且动态的。

但是,我也尝试将值传递给卡。

在json回复中,我确实得到了卡片

{
  "id": "REMOVED",
  "timestamp": "2017-12-05T11:10:52.033Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "weather in london",
    "action": "sayWeather",
    "actionIncomplete": false,
    "parameters": {
      "geo-city": "London"
    },
    "contexts": [],
    "metadata": {
      "intentId": "REMOVED",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "webhookResponseTime": 626,
      "intentName": "Weather"
    },
    "fulfillment": {
      "speech": "It is currently 9 degrees celcius in London.",
      "source": "agent",
      "displayText": "It is currently 9 degrees celcius in London.",
      "messages": [
        {
          "type": 0,
          "speech": "It is currently 9 degrees celcius in London."
        }
      ],
      "data": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": "This is the first simple response for a basic card"
            }
          },
          {
            "basicCard": {
              "title": "Title: this is a title",
              "formattedText": "This is a basic card.  Text in a\n      basic card can include \"quotes\" and most other unicode characters\n      including emoji 📱.  Basic cards also support some markdown\n      formatting like *emphasis* or _italics_, **strong** or __bold__,\n      and ***bold itallic*** or ___strong emphasis___ as well as other things\n      like line  \nbreaks",
              "subtitle": "This is a subtitle",
              "image": {
                "url": "https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png",
                "accessibilityText": "Image alternate text"
              },
              "buttons": [
                {
                  "title": "This is a button",
                  "openUrlAction": {
                    "url": "https://assistant.google.com/"
                  }
                }
              ]
            }
          },
          {
            "simpleResponse": {
              "textToSpeech": "This is the 2nd simple response ",
              "displayText": "This is the 2nd simple response"
            }
          }
        ]
      }
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success",
    "webhookTimedOut": false
  },

  "sessionId": "REMOVED"
}

使用 data.result.fulfillment.speech 访问语音的值(value)效果很好。

但是,当使用data.result.fulfillment.data.items.basicCard.image.url时,它不起作用。如果我上升几个级别,我确实会得到:

[object Object]

感谢您的帮助。

最佳答案

items 属性是一个列表,而不是一个对象。因此,您必须使用数字索引来检索数据。在您提供的示例中,basicCard 对象的索引是第二位,因此您的代码应如下所示:

data.result.fulfillment.data.items[1].basicCard.image.url

注意items后面的[1]

请记住,如果此列表的顺序发生更改,您可能不再检索 basicCard 对象,因此您可能需要添加一些检查以确保检索到所需的数据。

关于javascript - Dialogflow html/js 卡 json 值 v1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47652609/

相关文章:

javascript - 单击外部选择器触发事件

php - 关于 php 中的进度条

ruby-on-rails - 具有远程 Rails 服务器的 BackBone 客户端

json - 如何从instagram实时发布数据中获取实际照片?

javascript - ASP.NET:通过客户端 JavaScript 获取 ASP.NET TreeView 上选定节点的父节点文本

javascript - EmberJs 中的 Hello World 错误

javascript - AngularJs中如何保持页面数据刷新?

javascript - HTML/JS "Lock/Hide"内容直到日期设置

javascript - DOM 更改后,向后滚动,以便引用元素在屏幕上位于 DOM 更改发生之前的同一位置

javascript - 如何将对象引用传递给指令?