I have a json in this format:
{
"success": 1,
"value": "[\"1-1\",\"1-2\",\"1-3\"]"
}
The success is actually in int
and value is a List
of String
, i want to get the value and put those elements into a List<String>
. The List of String will be:
1-1
1-2
1-3
I actually wish to parse the entire json using JsonNode, but the problems that restrict me from doing so are:
"[\"1-1\",\"1-2\",\"1-3\"]"
is wrapped with double quotes, so it might be treated as String.- Have to get rid of the backslash in the string
The following is my workaround that i tried, is there any elegant/better (It is better without regex, just parse entire json into JsonNode) way to do so?
ObjectMapper mapper= new ObjectMapper();
JsonNode response = mapper.readTree(payload); //payload is the json string
JsonNode success = response.get("success");
JsonNode value = response.get("value");
if (success.asBoolean()) {
String value = value .toString();
value = value .replaceAll("\"", "").replaceAll("\\[", "").replaceAll("\\]", "")
.replace("\\", "");
return Arrays.asList(value .split(","));
}