Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:57:48+00:00 2026-05-23T02:57:48+00:00

I’m writting a program in Java that makes requests to an external API by

  • 0

I’m writting a program in Java that makes requests to an external API by calling some methods. Each of these methods returns a diferent JSON structure and it also depends on the passed parameters. My question is if there’s some way to deserialize the response string using Gson without having to write a different class for each method.

If not Gson, which other library could I use?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:57:49+00:00Added an answer on May 23, 2026 at 2:57 am

    My question is if there’s some way to deserialize the response string using Gson without having to write a different class for each method.

    Yes, there is. If the JSON structure that you don’t care to deserialize into a specific Java type is just an object that contains key-value pairs, where all of the values are JSON primitives, then the structure can be simply deserialized into a Map<String, String>.

    Note that a Map<String, Object> would not work as it does not provide enough type information for Gson to deserialize into. Gson would just create plain Object instances for each value — not String or Integer instances, but just plain Object instances. The actual values in the JSON would not be deserialized. On the surface it seems reasonable that Gson could be enhanced to create more type-specific instances for JsonPrimitive values. For example, since it’s possible to determine whether a JsonPrimitive is a Boolean, with a call to isBoolean(), then one might hope Gson would just construct a Boolean, but it doesn’t — Gson just creates an empty and probably-useless Object.

    Here’s an example that demonstrates this point.

    input.json Contents:

    {
      "one":"won",
      "too":22,
      "3":false
    }
    

    Foo.java:

    import java.io.FileReader;
    import java.lang.reflect.Type;
    import java.util.Map;
    
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
        Type mapOfStringObjectType = new TypeToken<Map<String, String>>() {}.getType();
        Map<String, String> map = gson.fromJson(new FileReader("input.json"), mapOfStringObjectType);
        System.out.println(map);
      }
    }
    

    The output of this example is {3=false, one=won, too=22}.

    If the Map type in the example is changed to HashMap<String, Object> as recommended in another answer, then the output becomes {3=java.lang.Object@10b61fd1, one=java.lang.Object@24e2dae9, too=java.lang.Object@299209ea}, which is of course probably useless.

    If for some reason a type of HashMap<String, Object> must be deserialized to, then custom deserialization processing would be necessary.

    If the JSON structure is not a simple set of key-value pairs, where the values are all JSON primitives, but include values that are complex types like JSON objects, even if it’s acceptable to transform all of the values into Strings, then it would also be necessary to implement custom deserialization processing.

    Don’t hesitate to head on over to the Gson Issues List and submit an enhancement request for improved JSON primitive deserialization to appropriately specific Java Object types, or to have a simple mechanism to transform any value — including complex JSON types like objects and arrays — in a Map into a String, such that custom deserialization processing as described above wouldn’t be necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.