GSON encoding the string like \u003d

apache

This tutorial explains how to handle the encoding issue with using GSON. Below is a sample programme 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 the what was supposed to be.

	Json result - sdswSSEdafsdy2y4\u003d\u003d

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