Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with Chris Kessel's answer chosen by CommunityBot
Notice added Draw attention by Yannis
Bounty Started worth 50 reputation by Yannis
added 6 characters in body
Source Link
Thilo
  • 263.7k
  • 107
  • 527
  • 674

I have an Api that returns JSON. The response is in some format that can fit into an object called ApiResult and contains a Context andContext <T> and an int Code.

ApiResult is declared in a generic way, e.g. ApiResultApiResult<SomeObject>

I would like to know how to get GSON to convert the incoming JSON String to ApiResultApiResult<T>

So far I have:

Type apiResultType = new TypeToken<ApiResult<T>>() { }.getType(); ApiResult<T> result = gson.fromJson(json, apiResultType); 

But this still returns converts the Context to a LinkedHashMap instead (which I assume its what GSON falls back to)

I have an Api that returns JSON. The response is in some format that can fit into an object called ApiResult and contains a Context and an int Code.

ApiResult is declared in a generic way, e.g. ApiResult

I would like to know how to get GSON to convert the incoming JSON String to ApiResult

So far I have:

Type apiResultType = new TypeToken<ApiResult<T>>() { }.getType(); ApiResult<T> result = gson.fromJson(json, apiResultType); 

But this still returns converts the Context to a LinkedHashMap instead (which I assume its what GSON falls back to)

I have an Api that returns JSON. The response is in some format that can fit into an object called ApiResult and contains a Context <T> and an int Code.

ApiResult is declared in a generic way, e.g. ApiResult<SomeObject>

I would like to know how to get GSON to convert the incoming JSON String to ApiResult<T>

So far I have:

Type apiResultType = new TypeToken<ApiResult<T>>() { }.getType(); ApiResult<T> result = gson.fromJson(json, apiResultType); 

But this still returns converts the Context to a LinkedHashMap instead (which I assume its what GSON falls back to)

Source Link
Yannis
  • 6.2k
  • 5
  • 47
  • 63

Convert JSON String to generic object in JAVA (with GSON)

I have an Api that returns JSON. The response is in some format that can fit into an object called ApiResult and contains a Context and an int Code.

ApiResult is declared in a generic way, e.g. ApiResult

I would like to know how to get GSON to convert the incoming JSON String to ApiResult

So far I have:

Type apiResultType = new TypeToken<ApiResult<T>>() { }.getType(); ApiResult<T> result = gson.fromJson(json, apiResultType); 

But this still returns converts the Context to a LinkedHashMap instead (which I assume its what GSON falls back to)