Question

Alright, so, I have a game that uploads data in the form of JSON to my site. The JSON resembles this:

{
  "PlayerList":[
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    },
    {
        "PlayerKills":0,
        "Username":"Player1",
        "TimePlayed":0,
        "Deaths":0
    }
  ]
}

After confirming that the JSON is indeed correct, and without errors, I began speculating that the problem lie in the PHP. The code I use to get the JSON is:

$decodedJSON =  json_decode($entityBody, true, 4);              
var_dump($decodedJSON);

With $entitybody being the JSON as a string.

The var_dump here returns NULL, and since I'm stuck using PHP 5.2, I cannot determine what the problem is using json_last_error.

So if anyone can provide me some info as to where the problem lies, it would be much appreciated.

No correct solution

OTHER TIPS

Try this:

$entityBody = stripslashes($entityBody);
// this will remove all backslashes which might be the cause of returning NULL

$decodedJSON = json_decode($entityBody, true);
// leave out the depth unless you really need it to be 4.

var_dump($decodedJSON);

Documentation:

stripslashes - http://php.net/manual/en/function.stripslashes.php

json_decode - http://php.net/json_decode

Don't set the depth parameter. Just json_decode($entityBody,true); should work.

In PHP 5.2 json_decode requires 2 parameters not 3. I checked it on PHP 5.2.17 and it shows:

Warning: json_decode() expects at most 2 parameters, 3 given. 

If you omit third parameter you will get what you want :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top