How to change the Encounter in Java?

Asked

Viewed 289 times

3

I am consuming the IBGE API to get the municipalities by state. When I print the municipalities the accents get wrong. What can I do to fix it? Below the code that takes the name of municipalities.

JSONArray jsonarray = new JSONArray(jsonString);
List municipios = new ArrayList();
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
        municipios.add(jsonobject.getString("nome").);
}

1 answer

4


I believe you can do this when adding text to the list:

JSONArray jsonarray = new JSONArray(jsonString);
List municipios = new ArrayList();
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    municipios.add(new String(jsonobject.getString("nome").getBytes(), "UTF-8"));
}

Catching the bytes of the word and creating a string format UTF-8, accents and characters will be displayed.

  • It worked! Thank you, man!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.