I am trying to use PHP's json_decode function to get data from the database (phpmyadmin)(the type is set to text) and set it on the page This is the code that i got:
$belangrijkespecs = $productClass->get('belangrijkeSpecs');
$belangrijkespecs = json_decode($belangrijkespecs);
var_dump($belangrijkespecs);
This code outputs
NULL
And when I echo the $productClass->get('belangrijkeSpecs') it outputs:
{"Beeldschermdiagonaal":"10,1 inch (25,7 cm)","Beeldresolutie":"1920 x 1200","Batterijduur":"Tot 12 uur","Gewicht":"525 g","Opslag":"32 GB"}
When I passed this in an online JSON decode website than I get the right array (from the website https://3v4l.org/IHKZZ):
array (
'Beeldschermdiagonaal' => '10,1 inch (25,7 cm)',
'Beeldresolutie' => '1920 x 1200',
'Batterijduur' => 'Tot 12 uur',
'Gewicht' => '525 g',
'Opslag' => '32 GB',
)
When i try json_last_error() with this code:
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
It outputs
- Syntax error, malformed JSON
And if i just use echo json_last_error() it outputs : 4.
i have already tried :
$belangrijkespecs = str_replace("/", "", $belangrijkespecs);
and
$belangrijkespecs = rtrim($belangrijkespecs, "\0");
and
$belangrijkespecs = stripslashes($belangrijkespecs);
the output of var_dump $productClass->get('belangrijkeSpecs') :
string(240) "{"Beeldschermdiagonaal":"10,1 inch (25,7 cm)","Beeldresolutie":"1920 x 1200","Batterijduur":"Tot 12 uur","Gewicht":"525 g","Opslag":"32 GB"
The output of echo addcslashes($belangrijkespecs, '\0..\37!@\177..\377') is:
{"\;\Beeldschermdiagonaal"\;\:"\;\1\0,\1 inch (\2\5,\7 cm)"\;,"\;\Beeldresolutie"\;\:"\;\1\9\2\0 x \1\2\0\0"\;,"\;\Batterijduur"\;\:"\;\Tot \1\2 uur"\;,"\;\Gewicht"\;\:"\;\5\2\5 g"\;,"\;\Opslag"\;\:"\;\3\2 \G\B"\;}
And when i do
$belangrijkespecs = stripslashes($belangrijkespecs); before json_decode it still dosnt work...
Also tried:
$belangrijkespecs = preg_replace('/\\\\/', '', $belangrijkespecs);
But it did not work.
The structure of this 'belangrijkeSpecs' = http://prntscr.com/lvnsbh
I have looked on the internet but the answers that were given did not help me.
So my question is: How can i get a array from the json encoded string ( $productClass->get('belangrijkeSpecs') )
and I expect an array as a return from the json_decode();
echo $belangrijkespecs = $productClass->get('belangrijkeSpecs');$productClass->get('belangrijkeSpecs')is not actually returning what you think it is.var_dump($belangrijkespecs);(before attempting to decode it) would confirm. Note that reusing/reassigning variables ($thing = method($thing);) makes debugging harder - best avoided.