0

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();

24
  • remove the echo from this line echo $belangrijkespecs = $productClass->get('belangrijkeSpecs'); Commented Dec 16, 2018 at 14:58
  • No did not work. :( Commented Dec 16, 2018 at 15:01
  • It is far from clear what your issue is and what you are actually trying to do Commented Dec 16, 2018 at 15:01
  • 1
    It is still not clear what your actual problem is Commented Dec 16, 2018 at 15:11
  • 1
    Since the json string in the question decodes fine - it is most likely that $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. Commented Dec 16, 2018 at 15:11

1 Answer 1

-1

The awnser was that i escaped the string before i did the json_decode. And because i did that there where a lot of backslashes.

Thanks to @Matthijs i found it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.