0

I am going through a problem when I call my generic static method, the type is not inferred. I don't have the type reference in the method arguments.

public static <K> K parseJsonToObject(String JsonString) {
    Gson gson = new Gson();
    K k = gson.fromJson(JsonString, new TypeToken<K>() {
    }.getType());
    return k;
}

and calling with

homeContent = JsonHelper.<HomeContent>parseJsonToObject(result);

and got the crash log

illegal type variable reference

need some help here. thanks

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • If the type could be inferred like that, `TypeToken` wouldn't be necessary in the first place. You have to explicitly initialize it with the correct type. Or just pass a `Class` in your case. – shmosel Apr 24 '18 at 03:45
  • thanks, i passed a class and it's working – Chen Adam Apr 24 '18 at 04:24

0 Answers0