【发布时间】:2019-03-14 19:44:13
【问题描述】:
我正在尝试将我的 hashmap 转换为 JSON 漂亮的打印输出。
我尝试了 GSON,并认为它在将字符串作为地图中的输入处理时存在一些问题,有没有其他方法可以做到这一点?
Map<String, String> map = new LinkedHashMap();
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create();
map.put("Intro", Map_to_String);
map.put("Output", String_Val);
System.out.println(gson.toJson(map));
输出:
{
"Intro": {\"No\":0,\"Cast\":2},
"Output": "123"
}
所需输出:
{
"Intro": {"No":0,"Cast":2},
"Output": "123"
}
【问题讨论】:
-
{"Intro": "{"No":0,"Cast":2}","Output": "123"}- 无效JSON。您确定不想像我的回答那样{"Intro": {"No": 0,"Cast": 2},"Output": "123"}吗? -
是的,对不起,我会更新相同的。