gpt4 book ai didi

php - 转换编码的 utf-8 JSON

转载 作者:行者123 更新时间:2023-11-29 20:58:14 25 4
gpt4 key购买 nike

我使用此 PHP 代码从 MySQL 数据库生成 JSON:

    while($row =mysqli_fetch_assoc($result))
{
$emparray[] = array_map('utf8_encode', $row);
}
echo json_encode($emparray);

然后我使用 HttpUrlConnection 来获取 url 并获取 Echo 字符串:

  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);
}

打印内容是:

[{"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"}]

我知道输出是经过编码的,因为我有 $emparray[] = array_map('utf8_encode', $row); 但如果我不使用它,具有特殊字符的文本就会变成空。

如何转换

"titulo":"C\u00f3digo de Defesa do Consumidor"

"titulo":"Código de Defesa do Consumidor"

我已经尝试过:

 new String(result.getBytes(), "UTF-8");

但没有任何改变

最佳答案

您可以使用 StringEscapeUtils Apache 库 https://commons.apache.org/proper/commons-lang/download_lang.cgi

为了将 unicode 保存在数据库中,您必须对它们进行编码。因此,通过像这样的编码将您的unicodes发送到服务器

StringEscapeUtils.escapeJava("Código de Defesa do Consumidor")

用于在android中显示编码的unicode

StringEscapeUtils.unescapeJava("C\u00f3digo de Defesa do Consumidor")

关于php - 转换编码的 utf-8 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com