简体   繁体   中英

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

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.

Looking at the response headers from getting that URL in my browser, the response is sent as UTF-8.

You should explicitly specify the charset when you create your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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