Problem reading json file using PHP

Asked

Viewed 80 times

0

I’m trying to save and search information inside a json file using php, my problem is that when I add more than one record in the file, when I use json_decode is returned to me null. I want to know if the problem is happening when I input the data, or if that’s when I read this data.

users.json content:

{"usuarios":{"username":"Lucas","data":"30\/04\/19"}},{"usuarios":{"username":"csa","data":"30\/04\/19"}}

Class responsible for collecting information

<?php

// Essa classe tem 2 funçoes salvar o usuario ou retornar os dados do usuario
class serverControl{
    private $path;

    public function __construct(){
        $path = dirname(__FILE__) . '\\';
        $this->path = $path;
    }

    public function buscaDados($arq){
        if(empty($arq)):
            return 'Erro parametros passados são invalidos';
        endif;

        $arq = $this->path . $arq . '.json';

        if(!file_exists($arq)):
            return 'Erro arquivo '.$arq.' não pode ser lido ou não exite';
        endif;

        $arq = file_get_contents($arq);

        $arqArray = json_decode($arq, true);

        return $arqArray;
    }

    public function salvarDados($d, $i,$arq){
        if(empty($d) & empty($i) & empty($arq)):
            return 'Erro Parametros passados são invalidos';
        endif;

        $arq = $this->path . $arq . '.json';

        if (!file_exists($arq)):
            return 'O arquivo '.$arq .' a ser modificado não exite';
        endif;

        // Adiciona o identificador
        $dados = array($i => $d);

        // Tranforma o array $dados_identificador em JSON
        $dados_json = json_encode($dados);

        // "a" indicar que o arquivo é aberto para ser escrito
        $fp = fopen($arq, "a");

        // Escreve o conteúdo JSON no arquivo
        $escreve = fwrite($fp, $dados_json);

        // Fecha o arquivo
        fclose($fp);
        return true;
    }
}
  • This JSON is not valid... what comes in this $i => $d?

  • The user identifier

1 answer

0


According to the colleague’s comment, JSON is invalid. As it is an array, it is missing the [], the correct would be:

[{"usuarios":{"username":"Lucas","data":"30\/04\/19"}},{"usuarios":{"username":"csa","data":"30\/04\/19"}}]

Here is an example:

<?php
$variavel = '{"usuarios":{"username":"Lucas","data":"30\/04\/19"}},{"usuarios":{"username":"csa","data":"30\/04\/19"}}';
$arqArray = json_decode($variavel, true);
echo ('teste 1<br>');
print_r($arqArray);
echo ('<hr>');

$variavel2 = '[{"usuarios":{"username":"Lucas","data":"30\/04\/19"}},{"usuarios":{"username":"csa","data":"30\/04\/19"}}]';
$arqArray2 = json_decode($variavel2, true);
echo ('teste 2<br>');
print_r($arqArray2);
echo ('<hr>');
?>

"teste1" returns empty, and "teste2", already with the change, returns the data

A tip: To validate whether your JSON is valid, you can use an online validator, here’s an example:

http://json.parser.online.fr

Browser other questions tagged

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