Skip to content Skip to sidebar Skip to footer

Instance Of Class In Java In Order To Use In Json Parser

I am using the json google library. we can have this: Public Class JsonParsing { private Gson gson; public JsonParsing(GsonBuilder builder) { this.

Solution 1:

You could change your method to:

public T fromJsonFromArray(String json, Class<T> classOfT) {
    return gson.fromJson(json, classOfT);
}

and call it like (for example):

fromJsonFromArray(someString, new Byte[0].getClass());

Note: it strikes me that this should probably be a generic method like so:

public <T> T fromJsonFromArray(String json, Class<T> classOfT) {
    return gson.fromJson(json, classOfT);
}

If you rely on the type parameter from the enclosing type (which your first example does), I don't see the purpose of taking a classOfT argument.

Solution 2:

Try using TokenType.

Something like?

TypeyourType=newTypeToken<YourClass[]>() {}.getType();

Post a Comment for "Instance Of Class In Java In Order To Use In Json Parser"