简体   繁体   English

Json解析重音字符

[英]Json parsing accent characters

The issue I am facing is that when I parse a character with an accent, my program will spit out random gibberish for that character. 我面临的问题是,当我解析带有重音符号的字符时,我的程序将为该字符吐出乱码。 Is there any way I can parse the characters such that it will it parse out and accent character instead? 有什么办法可以解析字符,以便解析出来并重音字符?

Parsing Céline Dion => Céline Dion 解析Céline Dion => Céline Dion

String fullURLPath = "https://itunes.apple.com/search?term=" + songInfoQuery.replace(" ", "+");

        System.out.println("!" + fullURLPath.toString());

        URL url = new URL(fullURLPath);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.connect();

        JsonParser jp = new JsonParser();
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
        JsonObject rootobj = root.getAsJsonObject();
        JsonArray arr = rootobj.getAsJsonArray("results");
        try {
            rootobj = arr.get(0).getAsJsonObject();
        } catch (IndexOutOfBoundsException e) {
            System.out.println("not in itunes");
        }

The problem is that you are parsing the data being sent back to you using your JVM's default charset, which is different from the charset used to encode the response. 问题是您正在使用JVM的默认字符集来解析发送回给您的数据,该字符集不同于用于编码响应的字符集。

Looking at the response headers from getting that URL in my browser, the response is sent as UTF-8. 通过在我的浏览器中获取该URL来查看响应标头,该响应以UTF-8格式发送。

You should explicitly specify the charset when you create your InputStreamReader : 创建InputStreamReader时,应显式指定字符集:

new InputStreamReader((InputStream) request.getContent(), StandardCharsets.UTF_8)

You could alternatively specify UTF-8 as your default charset when starting your JVM, but it is easy to forget to do that - it is better to be explicit in your code. 您也可以在启动JVM时将UTF-8指定为默认字符集,但是这样做很容易忘记-最好在代码中明确显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM