Home > Java Core > GSON encoding the string like \u003d

GSON encoding the string like \u003d

apache

This tutorial explains how to handle the encoding issue by using GSON. Below is a sample program in which GSON was converting string, with some special character, into some encoded character.

Map<String, String> dataMap = new HashMap<String, String>(); 
dataMap.put("userid", "sdswSSEdafsdy2y4=="); 
Gson gson = new Gson(); 
String json = gson.toJson(dataMap); 
System.out.println("Json result - "+json);

But when we printed the GSON string it gave some different output other than what was supposed to be.

Json result - sdswSSEdafsdy2y4\u003d\u003d

The issue with the above code is GSON is encoding my string “=” sign to \u003d. To handle this we need to use disableHtmlEscaping() with GSON, like the way it is written below –

Solution:

Map<String, String> dataMap = new HashMap<String, String>(); 
dataMap.put("userid", "sdswSSEdafsdy2y4=="); 
Gson gson = new GsonBuilder().disableHtmlEscaping().create(); 
String json = gson.toJson(dataMap); 
System.out.println("Json result - "+json);