if i am decoding a array with php i get JSON with brackets:
$data = array();
$data[0]["id"] = 23;
$data[0]["modul"] = "my modul";
echo '' .json_encode($data). '';
i'm getting a JSON like this:
[{"id":23,"modul":"my modul"}]
when i want this to decode with javascript there is nothing to decode cause javascript wants JSON without brackets
var myObj = JSON.parse(mySendJSONData);
i already tried to use this before parsing:
myObj = myObj[0];
but it did't solve it.
more clearly: i want to use server side events to send JSON to the client:
SERVER:
<?php
header( 'Content-Type: text/event-stream' );
header( 'Cache-Control: no-cache' );
$data = array();
$data[0]["id"] = 23;
$data[0]["modul"] = "my modul";
echo 'data: ' .json_encode($data). "
";
flush();
?>
CLIENT:
var source = new EventSource("myServerSideEvent.php");
source.onmessage = (event) => {
var myObj = JSON.parse(event.data);
document.getElementById("moduldialog").innerHTML = myObj.modul;
}