英文:
Serializing json string without escaping quote in java
问题
我有一个包含字符串列表 JSON 的字符串变量。我想将其作为值添加到映射中,以表示为字符串列表值。更具体地说:
String jsonListString = "[\"A\", \"B\"]";
Map<String, String> map = new HashMap();
map.put("KEY", jsonListString);
String serialized = new ObjectMapper().writeValueAsString(map);
System.out.println("Example:" + serialized);
输出如下:
Example:{"KEY":"[\"A\", \"B\"]"}
我期望的输出没有转义引号:
Example:{"KEY":["A", "B"]}
英文:
I have a string variable that contains json of list of strings. And I want to add this as a value to a map for representing as string list value. To be more specific:
String jsonListString = "[\"A\", \"B\"]";
Map<String, String> map = new HashMap();
map.put("KEY", jsonListString);
String serialized = new ObjectMapper().writeValueAsString(map);
System.out.println("Example:" + serialized);
The output is the following:
Example:{"KEY":"[\"A\", \"B\"]"}
I'm expecting without escaping quotes
Example:{"KEY":["A", "B"]}
答案1
得分: 1
目前您正在将一个字符串分配为KEY的值。如果您想改为分配一个数组,则它实际上需要是一个数组。
示例:
Map<String, String[]> map = new HashMap();
map.put("KEY", new String[]{"A", "B"});
英文:
Currently you are assigning a string as the value to KEY. If you want to assign an array instead it needs to actually be an array.
Ex
Map<String, String[]> map = new HashMap();
map.put("KEY", new String[]{"A", "B"});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论