I have this simple JSON object:
[{"k":51.39920565355378,"B":0.087890625}]
I want to access to k and B with Javascript, here is my code:
var jsonData = JSON.parse(jsonText);
console.log(jsonData.k); //Undefined
It always return me undefined, what ever I try but when I display jsonData, I can see the all description of the JSON object:
console.log(jsonData) //[{"k":51.39920565355378,"B":0.087890625}]
Here is the server code witch retrieve my JSON object:
function getMarkersByTripId($tripId)
{
if ($bdd = mysqli_connect(_BDD_HOST_, _BDD_USERNAME_, _BDD_PASSWORD_, _BDD_NAME_)) {
$sql = 'SELECT DISTINCT `markers` FROM `trip` WHERE `trip_id` = "'.$tripId.'"';
$req = mysqli_query($bdd, $sql);
if ($req) {
$row = mysqli_fetch_row($req);
echo json_encode($row[0]);
}
else {
echo json_encode(array('status' => 'failure'));
}
}
if ($bdd) {
mysqli_close($bdd);
}
}
Function witch add my JSON object into the database:
function actionSaveNewtrip($title, $status, $markers)
{
if ($bdd = mysqli_connect(_BDD_HOST_, _BDD_USERNAME_, _BDD_PASSWORD_, _BDD_NAME_)) {
$markers = mysqli_real_escape_string($bdd, $markers);
$sql = 'INSERT INTO `trip` (title, status, markers) VALUES("'.$title.'", "'.$status.'", "'.$markers.'")';
$req = mysqli_query($bdd, $sql);
if ($req) {
echo json_encode(array('status' => 'success'));
}
else {
echo json_encode(array('status' => 'failure'));
}
}
if ($bdd) {
mysqli_close($bdd);
}
}
$markers is the JSON object that I insert into the database.
I would like to know what I'm doing wrong to access to k and B like an array, thank you.