jaeyong
jaeyong

Reputation: 9363

Serializing json string without escaping quote in java

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"]}

Upvotes: 0

Views: 564

Answers (1)

Deadron
Deadron

Reputation: 5289

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

Upvotes: 1

Related Questions