This question is scoped to The Java API for JSON Processing (JSR 353).
Given a JsonArray whose elements are JsonString, how can I easily convert this to a List<String>
or a Set<String>
? For Set<String>
, let's assume that the JsonArray contains a unique collection of string values.
Assume I have a JSON object like this:
{
"name" : "James Johns",
"street" : "22 Nancy St.",
"emails" : [
"james@a.com",
"james@b.net",
null,
""
]
}
I want my resulting collection to ignore any null or empty string entries in the emails array.
Let's assume my JsonObject instance has already been created.
JsonObject person = <parse the JSON input>;
JsonArray emails = person.get("emails");
I keep getting stumped on JsonValue and JsonString and trying to get the actual String value. If I use the JsonValue.toString()
and JsonString.toString()
methods, I get string values that include the double quotes, as if the original JSON was "\"james@a.com\""
. I need the string values to be the equivalent JSON value "james@a.com"
.