How do I allow accentuation feedback in JSON using PHP?

Asked

Viewed 633 times

2

Attached the photo of my return JSON

inserir a descrição da imagem aquiI have following code:

<?php
header("Content-Type: text/html; charset=UTF-8",true);

$array1 = array();
    $contador = 0;
    $estado="online";




if (!$enlace = mysql_connect('meuHost', 'meuUser', 'Minhasenha')) {
    echo '{}';
    exit;
}

if (!mysql_select_db('MinhaBaseDados', $enlace)) {
    echo '{}';
    exit;
}
mysql_set_charset("uft-8");


$sql = 'select distinct(param_curso.curso_designacao) as Universidade, count(candidato_curso.id) as Quantidade from param_curso 
inner join ciclocurso on param_curso.id=ciclocurso.curso_id
inner join candidato_curso on ciclocurso.id=candidato_curso.ciclo_curso_id
group by param_curso.curso_designacao;';

$resultado = mysql_query($sql, $enlace);


if (!$resultado) {
    echo "{}";

}

while ($fila = mysql_fetch_assoc($resultado)) {
   $validacoes =new Cursos_validacoes;
                        $validacoes->universidade = $fila["Universidade"];
                        $validacoes->quantidade = $fila["Quantidade"];

       if($validacoes->universidade==null){
           $validacoes->universidade="vazio";
       }

       $array1[$contador] = $validacoes;
                        $contador = $contador + 1;  



                        }
     echo json_encode($array1);
mysql_close($enlace);






class Cursos_validacoes{    
    public $universidade;
        public $quantidade;

}


?>
  • What is your problem? I didn’t quite understand what you meant by JSON accentuation return, the data is not coming in UTF8 database format ?

  • This is the problem the data does not come in UTF8 format, and I do not know why.

  • I advise you to use PDO, this mysql class is already deprecated, but if you use it you will need to set the charset utf, you can do something like $link = mysql_connect('localhost', 'user', 'password'); mysql_set_charset('utf8',$link);

  • Adding mysqli_query($myconnection, 'SET CHARACTER SET utf8'); before query select if I am wrong solve your problem.

  • It does not! Allow the return to be in the format encoded by the function and ready. JSON was created to facilitate the exchange of data between several languages through Javascript. The returned format has the encoding of.

1 answer

1

Utilizes the json_encode with the constant JSON_UNESCAPED_UNICODE to literally encode the characters instead of escaping them.

echo json_encode($array1, JSON_UNESCAPED_UNICODE);
  • Don’t understand your suggestion!!!

  • Simple, right where you stand - echo json_encode($array1); - puts - echo json_encode($array1, JSON_UNESCAPED_UNICODE);.

Browser other questions tagged

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