JSON badly formatted

Asked

Viewed 137 times

1

Hello, my friends.

I am generating a file . json with PHP, but even when it is created it is badly formatted, it is not possible to read its data. See:

{
"0": "6",
"idt1": "6",
"1": "2018-10-19",
"datahora": "2018-10-19",
"2": "1",
"resultado": "1",
"3": "1",
"indicbet": "1",
"4": "Inglaterra - Premier League 2",
"LIGA": "Inglaterra - Premier League 2",
"5": "",
"rh": "",
"6": "",
"ra": "",
"7": " Fulham SubWest Brom Sub",
"TIMEHA": " Fulham SubWest Brom Sub",
"8": "71",
"TARGLG": "71",
"9": "0",
"PLACH": "0",
"10": "2",
"PLACA": "2",
"11": "GREEN",
"RESULT": "GREEN"
}{
"0": "6",
"idt1": "6",
"1": "2018-10-19",
"datahora": "2018-10-19",
"2": "1",
"resultado": "1",
"3": "1",
"indicbet": "1",
"4": "Inglaterra - Premier League 2",
"LIGA": "Inglaterra - Premier League 2",
"5": "",
"rh": "",
"6": "",
"ra": "",
"7": " Fulham SubWest Brom Sub",
"TIMEHA": " Fulham SubWest Brom Sub",
"8": "71",
"TARGLG": "71",
"9": "0",
"PLACH": "0",
"10": "2",
"PLACA": "2",
"11": "GREEN",
"RESULT": "GREEN"
}

As you can see, the brackets are missing at the beginning and end and the commas between the "arrays".

Here the code that generates json:

while($rowJ = mysqli_fetch_array($resultJ)){ 
if(count($_items) > 1){
//criar JSON/tabela do USUARIO
                   // Escreve o resultado JSON em arquivo:
                   $idu = $_SESSION['idu_log_pn'];
                   $idu = 1;
                   $pasta = $idu;
                   $diretorio = "users/".$pasta;

                   if(!file_exists($diretorio))
                   {
                    mkdir("$diretorio", 0777);
                   }



// Tranforma o array $dados em JSON
$dados_json = json_encode($rowJ, JSON_PRETTY_PRINT);

// Cria o arquivo cadastro.json
// O parâmetro "a" indica que o arquivo será aberto para escrita
$fp = fopen("users/".$idu."/file".$idu.".json", "a");

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

// Fecha o arquivo
fclose($fp);
                  }

}

1 answer

5


Instead of open up, write down and close the file at each iteration in its loop of repetition, why not keep it open during the process?

Incidentally, the problem is that you are formatting for a JSON array associative and writing directly into the file in APPEND mode. That is, pro JSON this content has never been a list, so there is no reason for it to add the brackets, let alone the commas between the records. If you want to format to JSON a list, pass a list.

$data = [];

while ($rowJ = mysqli_fetch_array($resultJ)) {
  if(count($_items) > 1){
    // ...
    $data[] = $rowJ;
  }
}

$dados_json = json_encode($data, JSON_PRETTY_PRINT);

$fp = fopen("users/{$idu}/file{$idu}.json", "w");

$escreve = fwrite($fp, $dados_json);

fclose($fp);

So what you will be formatting for JSON is a array of objects, generating the expected result.

You can also use file_put_content to save the contents to the file instead of fopen, fwrite and fclose:

file_put_contents("users/{$idu}/file{$idu}.json", $dados_json);

Browser other questions tagged

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