I'm using javax.Json.JsonArray
object.
method: javax.Json.JsonArray.getvaluesAs(Class<T>)
returns the JsonArray
as List. So what I want to do is to get the List
into a Set
. this is what I do:
Set values= new HashSet(myjsonArray.getValuesAs(null));
This works fine, but I know that myjsonArray
only contains String
. so what I really want to do is :
Set<String> values= new HashSet(myjsonArray.getValuesAs(String.class));
For some reason for the above code I get compiler error:
The generic method getValuesAs(Class<T>) of type JsonArray is not applicable for the arguments (Class<String>). The inferred type String is not a valid substitute for the bounded parameter <T extends JsonValue>
I can get my Set<String>
built using loops, but I try to avoid that, so is there an easy fix for above error?
The getValuesAs() method only supports classes that extends JsonValue. In this case you can use JsonString.class which then casts simply to a String.
Check this (http://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html#getValuesAs-java.lang.Class-) and you could see that T extends JsonValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With