JSON does not convert special character?

1

I have a ArrayList and when I convert it to a Json using Gson the special characters do not match the serialization.

Obs My bank and project is in utf8 encoding.

I'm serializing ArrayList like this:

Cliente cliente = new Cliente();
ClienteDAO dao = new ClienteDAO();        
lista = dao.listaClientes(cliente,empresa,dataHoraExp,compartilhaBas);

Gson g = new Gson();
String json = g.toJson(lista);

In my%% change% where you should display json is displaying "bairro1":"NOSSA SRº DE FATIMA" .

How do I convert without losing the special characters?

    
asked by anonymous 21.03.2017 / 22:37

1 answer

1

Apparently your ide is pointing to another enconding.

If your project is using maven, simply add the following property in the .pom:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

If you are using gradle you will need to add:

compileJava.options.encoding = 'UTF-8'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

Now if you are not using any dependency manager you will need to set the settings in your IDE. If you are using eclipse you will need to go to:

Window -> Preferences -> General -> Workspace : Text file encoding and change the encoding to utf-8 .

If you use netbeans you will need to go to the file netbeans.conf and set -J-Dfile.encoding=UTF-8

Another option is in its class main in the first execution line setar: System.setProperty("file.encoding", "UTF-8");

    
21.03.2017 / 23:33