gpt4 book ai didi

javascript - 只显示特定的json信息

转载 作者:行者123 更新时间:2023-12-03 03:52:04 26 4
gpt4 key购买 nike

我对javascript不太熟悉。我会知道如何只显示特定信息

结果显示完整的json信息,但我只想要答案

谢谢。

"fulfillment": {
"speech": "Sorry, can you say that again?",
"messages": [
{
"type": 0,
"speech": "Sorry, can you say that again?" ==> answer
}
]

我的 JavaScript

<html>
<head>
<title>API Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var accessToken = "xxxxxxxxxxxxxxxxxxx";
var baseUrl = "https://api.api.ai/v1/";
$(document).ready(function() {
$("#input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
send();
}
});
$("#rec").click(function(event) {
switchRecognition();
});
});
var recognition;
function startRecognition() {
recognition = new webkitSpeechRecognition();
recognition.onstart = function(event) {
updateRec();
};
recognition.onresult = function(event) {
var text = "";
for (var i = event.resultIndex; i < event.results.length; ++i) {
text += event.results[i][0].transcript;
}
setInput(text);
stopRecognition();
};
recognition.onend = function() {
stopRecognition();
};
recognition.lang = "en-US";
recognition.start();
}

function stopRecognition() {
if (recognition) {
recognition.stop();
recognition = null;
}
updateRec();
}
function switchRecognition() {
if (recognition) {
stopRecognition();
} else {
startRecognition();
}
}
function setInput(text) {
$("#input").val(text);
send();
}
function updateRec() {
$("#rec").text(recognition ? "Stop" : "Speak");
}
function send() {
var text = $("#input").val();
$.ajax({
type: "POST",
url: baseUrl + "query?v=20150910",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
data: JSON.stringify({ query: text, lang: "en", sessionId: "somerandomthing" }),
success: function(data) {
setResponse(JSON.stringify(data, undefined, 2));
},
error: function() {
setResponse("Internal Server Error");
}
});
setResponse("Loading...");
}
function setResponse(val) {
$("#response").text(val);
}
</script>
<style type="text/css">
body { width: 500px; margin: 0 auto; text-align: center; margin-top: 20px; }
div { position: absolute; }
input { width: 400px; }
button { width: 50px; }
textarea { width: 100%; }
</style>
</head>
<body>
<div>
<input id="input" type="text"> <button id="rec">Speak</button>
<br>Response<br> <textarea id="response" cols="40" rows="20"></textarea>
</div>
</body>
</html>

结果

{
"id": "2fc6a6b2-9fab-4045-b1cf-xxxxxxxxxxxxx",
"timestamp": "2017-07-15T15:28:54.34Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Could you tell me something ?",
"action": "input.unknown",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "c9376a7d-ab8b-4304-8c84-xxxxxxx",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Default Fallback Intent"
},
"fulfillment": {
"speech": "Can you say that again?",
"messages": [
{
"type": 0,
"speech": "One more time?"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "somerandomthing"
}

最佳答案

如果服务器答案是 JSON,您所需要做的就是使用您想要的属性调用 setResponse 函数,在您的情况下为语音

setResponse(data.result.fulfillment.messages[0].speech);

如果您已显示传递给函数的参数将是:“再来一次?”

关于javascript - 只显示特定的json信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45119945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com