[英]Can't parse DialogFlow json response
I know this question has been asked multiple times... but I can't figure out what happens I've an issue while trying to parse a json response. 我知道这个问题已经被问过多次了……但是当我试图解析一个json响应时,我不知道发生了什么问题。 Here is the json (extract)
这是json(摘录)
{"result":{"fulfillment":{"speech":"[{\"name\":\"Pallet truck\",\"object_type\":\"machine\",\"object_id\":3279}, ...
I'm applying JSON parse on it like this 我正在像这样应用JSON解析
var obj = JSON.parse(response);
Then I have something like this 然后我有这样的东西
"result":{
"fulfillment":{
"speech":"[
{"name":"Pallet truck","object_type":"machine","object_id":3279},
{"name":"CollaborativeRobot","object_type":"machine","object_id":3273},
{"name":"Bender","object_type":"machine","object_id":3997},...
I want only display it like that at the end : 我只想在最后显示这样:
Name : Pallet Truck
Name : CollaborativeRobot
Name : Bender
I've tried things like this 我已经尝试过这样的事情
for (var key in obj.result.fulfillment.speech) {
if (obj.result.fulfillment.speech.hasOwnProperty(key)) {
console.log(obj.result.fulfillment.speech[key].name.val());
}
}
But I only got undefined results... I think I'm missing something while accessing the array (it's been a while without coding anything in php/js ) 但是我只有未定义的结果...我想我在访问数组时丢失了一些东西(已经有一段时间没有在php / js中进行任何编码了)
EDIT 编辑
EDIT 2 编辑2
It seams that the issue is on server side, while double encoding here is an extract : 它表明问题出在服务器端,而此处的双重编码是摘录:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.mephisto.optimdata.io/search?object_type=machine');
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'authorization: JWT '.$accessToken));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$fulfillment = new stdClass();
$fulfillment->speech = $result;
$result = new stdClass();
$result->fulfillment = $fulfillment;
$result->requete = "machines actives";
$responseQueryAPI = new stdClass();
$responseQueryAPI->result = $result;
echo json_encode($responseQueryAPI);
Maybe this is because the curl response is already json? 也许这是因为curl响应已经是json了吗? The curl response looks like this :
卷曲响应如下所示:
[
{
"name": "Pallet truck",
"object_type": "machine",
"object_id": 3279
},
{
"name": "Collaborative Robot",
"object_type": "machine",
"object_id": 3273
},
{
"name": "Bender",
"object_type": "machine",
"object_id": 3997
},
name
is just a string, so try to replace name
只是一个字符串,因此请尝试替换
for (var key in obj.result.fulfillment.speech) {
if (obj.result.fulfillment.speech.hasOwnProperty(key)) {
console.log(obj.result.fulfillment.speech[key].name.val());
}
}
to 至
for (var i = 0; i < obj.result.fulfillment.speech.length; ++i) {
console.log(obj.result.fulfillment.speech[i].name);
}
Edit: 编辑:
The response data looks wrong. 响应数据看起来错误。
I got syntax error with JSON.parse('{"result":{"fulfillment":{"speech":"[{\\"name\\":\\"Pallet truck\\",\\"object_type\\":\\"machine\\",\\"object_id\\":3279}]"}}}');
我在
JSON.parse('{"result":{"fulfillment":{"speech":"[{\\"name\\":\\"Pallet truck\\",\\"object_type\\":\\"machine\\",\\"object_id\\":3279}]"}}}');
. 。
Try to use JSON.parse('{"result":{"fulfillment":{"speech":[{"name":"Pallet truck","object_type":"machine","object_id":3279}]}}}');
尝试使用
JSON.parse('{"result":{"fulfillment":{"speech":[{"name":"Pallet truck","object_type":"machine","object_id":3279}]}}}');
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.