How to change the encode in Java?

3

I am consuming the IBGE API to get municipalities by state. When I print out the municipalities the accents are wrong. What can I do to fix this? Below the code that takes the name of the 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").);
}
    
asked by anonymous 06.11.2018 / 13:12

1 answer

6

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

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

    
06.11.2018 / 13:22