I'm using this PHP code to generate the JSON from MySQL database:
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = array_map('utf8_encode', $row);
}
echo json_encode($emparray);
Then I use the HttpUrlConnection to fetch the url and get the Echo string:
URL url = new URL("http://localhost/myserver.php");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
the print is:
[{"id":"1","titulo":"C\u00f3digo de Defesa do Consumidor","descricao":"Institui o c\u00f3digo de defesa do consumidor","tags":"cdc","categorias":"cat_cod,cat_leisord","numero_da_lei":"13105","data_da_lei":"11111111","ativa":"1","byuser":"0","versao_da_lei":"0","url":"http:\/\/www.planalto.gov.br\/ccivil_03\/leis\/L8078compilado.htm"}]
I know that the output is encoded because I have $emparray[] = array_map('utf8_encode', $row);
but if I do not use this, the text that has special chars becomes NULL.
How can I convert
"titulo":"C\u00f3digo de Defesa do Consumidor"
to
"titulo":"Código de Defesa do Consumidor"
I've tried:
new String(result.getBytes(), "UTF-8");
but nothing changed