RIP SunMicrosystem
RIP SunMicrosystem

Reputation: 426

Parsing HashMap to Json

Here is the HashMap I'm trying to parse:

HashMap map = new HashMap();  
map.put("bowser", "b=mozilla");  
map.put("car", "car=Ford");
map.put("model","model=Mustang");
map.put("Year", 2014);
map.put("dealer", "Dealer=AKHI");

First I've tried Gson and then Jackson but both of them have a common problem., They parse "=" to "\u003d"

ObjectMapper mapper = new ObjectMapper();

try {
    String json = mapper.writeValueAsString(map);
    System.out.println("---------------------Parsed HashMap---------------------------:"+json); 
}
catch (JsonGenerationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 
catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The output I get is :

---------------------Parsed HashMap---------------------------: {"dealer":"Dealer\u003dAKHI","car":"car\u003dFord","Year":2014,"model":"model\u003dMustang","bowser":"b\u003dmozilla"}

I've been through few other blogs and see that there is a glitch in the API but is there any way we could fix this here with may be some other method.

Upvotes: 3

Views: 700

Answers (1)

Kick
Kick

Reputation: 4923

The = sign is encoded to \u003d. Hence you need to use disableHtmlEscaping().

You can use

 Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Upvotes: 6

Related Questions