JSON doesn’t convert special character?

Asked

Viewed 763 times

1

I have a ArrayList and when will I convert him to a Json using the Gson special characters do not accompany serialization.

Obs My bank and project is in encoding utf8.

I am Serializing the ArrayList thus:

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 range json where it should present "bairro1":"NOSSA SRº DE FATIMA" is presenting "bairro1":"NOSSA SR? DE FATIMA ".

How do I convert without losing the special characters?

  • Which one do you use? vc is using Maven?

  • I’m using the Netbeans.

1 answer

1

Apparently your ide is pointing another enconding.

If your project is using Maven, just add in . pom the following property:

<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 the eclipse you will need to go in:

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

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

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

  • That was right, I did what guided me and gave it right. Thank you!!!

Browser other questions tagged

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