java转换UTF-8字符(è)问题

标签 java json

嗨,我遇到了 java 问题...json 正确到达字符串数组,但字符 (is) 是 A ...有人可以帮助我转换为 UTF-8 和字符串

谢谢

private static void loadProperties() throws Exception {
        String properties = null;
        HttpsURLConnection con = (HttpsURLConnection) new URL(getEndpoint()).openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer res = new StringBuffer();
        while((inputLine = in.readLine()) != null) {
            res.append(inputLine);
        }
        in.close();
        JsonObject jsonObject = new JsonParser().parse(res.toString()).getAsJsonObject();
        if(jsonObject.has("error")) {
            JsonObject error = jsonObject.get("error").getAsJsonObject();
            if(error.has("errorCode") && "1000".equals(error.get("errorCode").getAsString()) && jsonObject.has("data")) {
                properties = jsonObject.get("data").getAsJsonArray().toString();
                log("PropertiesUtils.loadProperties: Properties = %s", properties);
                setProperties(properties);
            } else {
                throw new Exception("PropertiesUtils.loadProperties: Error while calling properties endpoint");
            }
        } else {
            throw new Exception("PropertiesUtils.loadProperties: Error while calling properties endpoint");     
        }
    }

最佳答案

您的错误在这里:

new InputStreamReader(con.getInputStream())

这需要 InputStream 传输 HTTP 响应(即纯字节流),并使用默认编码将其重新解释为文本(因为您没有指定编码)。

默认编码完全取决于您的计算机、操作系统和设置,并且与远程服务器可能向您发送数据的编码完全无关。

您需要使用con.getContentEncoding()获取服务器通知您的编码:

new InputStreamReader(con.getInputStream(), con.getContentEncoding());

关于java转换UTF-8字符(è)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60753595/

相关文章:

java - 加载jar库 加载库

java - PHP - 使用 Exec 执行带有用户定义参数的 Jar 文件

php - 一个对象 json 中的两个查询 mysql

java - JSP:转发页面时出错

java - 如何忽略 "handler": {}, "hibernateLazyInitializer": {} in json jackson in Spring hibernate project?

java - 尽管是 "should not relied on",但 Object#finalize() 是否有任何正确用途?

java - 从 Java 发送电子邮件时 SMTP 服务器挂起时的帮助

java - 动态更改 Excel 中的单元格颜色

Java - 如何对 Set<Entry<K,V>> 进行排序?

java - 如何使用 Play 转换 Java 对象以供 jqplot 使用! 2.0?