这是我要解析的 HashMap:
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");
首先我尝试了 Gson,然后是 Jackson,但他们都有一个共同的问题。 他们将“=”解析为“\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();
}
我得到的输出是:
--------------------解析的HashMap------------------------ ---: {"dealer":"Dealer\u003dAKHI","car":"car\u003dFord","Year":2014,"model":"model\u003dMustang","bowser":"b\u003dmozilla"
我浏览了其他几个博客,发现 API 中存在故障,但我们有什么方法可以在这里解决这个问题,可能是其他方法。
最佳答案
=
符号被编码为 \u003d
。因此,您需要使用 disableHtmlEscaping()
。
你可以使用
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
关于java - 将 HashMap 解析为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21528963/