Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从api.ai的json响应数组中提取文本_Javascript_Jquery_Dialogflow Es - Fatal编程技术网

Javascript 从api.ai的json响应数组中提取文本

Javascript 从api.ai的json响应数组中提取文本,javascript,jquery,dialogflow-es,Javascript,Jquery,Dialogflow Es,我试图构建一个聊天机器人,从api.ai获取json响应。我需要显示json响应下面的消息部分 { "id": "ae66f8e4-a047-478a-8108-8b0147610f18", "timestamp": "2017-09-28T05:02:03.552Z", "lang": "en", "result": { "source": "agent", "resolvedQuery": "hi", "action": "", "actionIncomplete": false, "par

我试图构建一个聊天机器人,从api.ai获取json响应。我需要显示json响应下面的消息部分

{
"id": "ae66f8e4-a047-478a-8108-8b0147610f18",
"timestamp": "2017-09-28T05:02:03.552Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hi",
"action": "",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
  "intentId": "d2f3c8bd-fc1b-4b6b-9d3d-08b6be93364e",
  "webhookUsed": "false",
  "webhookForSlotFillingUsed": "false",
  "intentName": "greetings"
},
"fulfillment": {
  "speech": "Hi.Please enter your query.",
  "messages": [
    {
      "type": 0,
      "speech": "Hi.Please enter your query."
    }
  ]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
 },
 "sessionId": "saurabh"
}

下面是一段示例代码,但它无法从spokenResponse变量中的messages部分获取语音

   function prepareResponse(val) {
  var debugJSON = JSON.stringify(val,undefined, 2);
  var spokenResponse = val.messages.speech;

  respond(spokenResponse);
  debugRespond(debugJSON);

}
我得到以下错误:

 script.js:33 Uncaught TypeError: Cannot read property 'speech' of undefined
  at prepareResponse (script.js:33)
  at Object.success (script.js:21)
  at i (jquery-3.2.1.min.js:2)
  at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2)
  at A (jquery-3.2.1.min.js:4)
  at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4)

我不熟悉ajax和Json,请帮助。

您必须通过fulfillment属性访问消息。请注意,消息包含一个对象数组,因此为了访问第一个对象的speech属性,必须访问第一个索引中的对象

var spokenResponse = val.fulfillment.messages[0].speech;
如果您希望遍历所有消息(假设您有多条消息):

var messageArr=val.fulfillment.messages;
var speechArr=[];
对于(变量i=0;i

这将为您提供一系列语音消息。

'message'是“实现”的子对象。因此val.fulfillment.message[0]。speech应该可以正确访问该值。
.messages
似乎是一个数组,而不是您要查找的对象。
.messages[0]。speech
可能吗?我尝试使用var spokenResponse=val.fulfillment.messages[0]。speech;仍然是相同的错误script.js:33 Uncaught TypeError:无法将undefinedTry的属性“messages”读取到console.log(val)。是否将履行属性视为结果的一部分?var spokenResponse=val.result.fulfillment.messages[0]。speech;这是可行的,LennieCodes you rock.var spokenResponse=val.fulfillment.messages[0].speech;这也引发了同样的错误,我通过javascript从api.ai获取json响应。当我比较api.ai中的json响应和通过代码获取的json响应时,两者完全相同,但无法访问消息部分。我甚至试过var spokenResponse=val.fulfillment.speech;由于fulfillment past也有speech,但无法访问“无法读取未定义的属性‘messages’”意味着JSON对象中没有fulfillment属性。在你的函数中做一个console.log(val),看看它给了你什么。您看到履行属性了吗?在看到console.log在下面所做的更改及其工作后,感谢您向正确的方向提供指导。var spokenResponse=val.result.fulfillment.messages[0]。语音;
var spokenResponse = val.fulfillment.messages[0].speech;
var messageArr = val.fulfillment.messages;
var speechArr = [];
for (var i = 0; i < messageArr.length; i++) {
    speechArr.push(messageArr[i].speech);
}