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. Here is the json (extract)
{"result":{"fulfillment":{"speech":"[{\"name\":\"Pallet truck\",\"object_type\":\"machine\",\"object_id\":3279}, ...
I'm applying JSON parse on it like this
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 )
EDIT
EDIT 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? 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
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}]"}}}');
.
Try to use JSON.parse('{"result":{"fulfillment":{"speech":[{"name":"Pallet truck","object_type":"machine","object_id":3279}]}}}');
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.