Dialogflow html/js 卡片 json 值 v1

Dialogflow html/js card json value v1

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

在我的场景中,我向机器人询问 "weather in London",它通过 webhook 回复 "It is currently 9 degrees celcius in London."。

这是正确的和动态的。

不过,我也在尝试将这些值也传递给卡片。

在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 访问 speech 的值工作正常。

然而,当使用 data.result.fulfillment.data.items.basicCard.image.url 时,它就不起作用了。如果我上升几级,我会得到:

[object Object]

感谢您的帮助。

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

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

注意 items 之后的 [1]

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