PHP returning an invalid JSON

Asked

Viewed 80 times

0

Good morning.

I am working on a system where all Curl returns will be saved in a JSON at the end of the application to be used by the Mobile guys.

The system is all in PHP, basically it looks for data in a footstats API... The returned data is used in the system, but as soon as they come back in the request, I save them in an object...

The object is inside the public function __construct() {}

Follows code from construct:

public function __construct()
    {
        $this->jsonGeral = new \stdClass();
        $this->jsonHeatmap = new \stdClass();
    }

Every new request data is saved this way:

public function buscarJogo($idPartida)
    {
        $url = "partidas/".$idPartida;
        $response = $this->fazerCurl($url);
        $this->jsonGeral->dadosPartida = $response;
        $response = json_decode($response);
        return $response;
    }

the $response, is already coming as Json, so I just put it inside the object.

In the end, when all that was needed has already been done, I make the call to turn everything into Json:

public function gerarJsonGeral()
    {
        $jsonGeral = stripslashes(json_encode($this->jsonGeral));
        $data = date('d-m-Y-h-s-i');
        $arquivo = fopen("jsonGeral-".$data.".txt", 'w');
        if ($arquivo == false)
        {
            die('Não foi possível criar o arquivo.');
        }
        $texto = $jsonGeral;
        fwrite($arquivo, $texto);
        fclose($arquivo);

        return "jsonGeral-".$data.".txt";
    }

It will generate the file, apparently it will be correct, but when trying to validate, will give an error:

Erro Json

I have tried to modify and instead of an object, leave as GLOBAL $json and save everything in array form, but also did not work...

I don’t know what else to do and I’m kind of desperate to try to make it work... Can anyone notice a mistake or has a solution to fix it?

Thank you in advance.

1 answer

2


I believe the mistake lies in this line

$jsonGeral = stripslashes(json_encode($this->jsonGeral));

Update after the comment

In case, how jsonGeral if it is an object, use only the function json_encode. If you want to sanitize the data, treat it before converting to JSON, so it’s no problem.

Example:

$jsonGeral = json_encode($this->jsonGeral);

The function stripslashes undoes the effect of addslashes. The latter places a \ before the characters ' or ", while stripslashes remove these bars.

Sometimes the JSON generated can contain \". Therefore, the use of the function stripslashes would generate an invalid JSON.

Example:

echo json_encode('Wallace"');
//resultado: ""Wallace\"""

echo stripslashes( json_encode('Wallace"') )
// resultado: ""Wallace"""

If you want to identify the error in json, can use the function json_last_error or json_last_error_msg.

Thus:

$json_errado = stripslashes( json_encode('Wallace"') );

json_decode($json_errado); // null

json_last_error_msg();
// "Control character error, possibly incorrectly encoded"
  • It is returning an error: Warning: stripslashes() expects parameter 1 to be string, object given in C:\xampp\htdocs\footstats\src\Controller\CurlController.php on line 427 and the file is generated only with a null :/

  • Ah, I see the problem

  • 1

    And this was exactly the problem... Fight for the speed of help my dear :D

  • Not at all, young man. I made a few edits, in case I wanted to debug some details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.