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 6155787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:29:53+00:00 2026-05-23T20:29:53+00:00

I’m using Google’s GSON Library to convert JSON data to Java objects. The issue

  • 0

I’m using Google’s GSON Library to convert JSON data to Java objects. The issue is that class name of the Java objet is being passed in the JSON data as well, so it is available only as a string. I’m not too familiar with Java so I don’t understand how to declare an object when the class name is stored as a string.

Also, the same is the case for the method that I will be calling after the data is initialized.

Here’s the relevant code so far:

request = gson.fromJson( rawData.toString(), JSONRequest.class );

String method = request.getMethod();
String data = request.getData();
String dataClass = request.getDataClass();

// convert data into an object of dataClass and execute method by passing dataObject
dataClass dataObject = gson.fromJson( data, dataClass.class );
result = method( dataObject );

This seems like a very crude way to accomplish the data to object conversion. Is there a better way? If not, how can I fix the code?

  • 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-23T20:29:55+00:00Added an answer on May 23, 2026 at 8:29 pm

    With the example code in the original question, dataClass.class will always be String.class, which I doubt is the correct target type to deserialize to.

    I think the following is what was intended to be demonstrated. (I’m not sure what implementation of JSONRequest was used in the original question. I’ll just make one up.)

    If the target type is really not known, and it’s not a sub-type of some known parent type, then reflection would be necessary to invoke the method. (This assumes that the target method does not require any parameters.)

    import java.io.FileReader;
    import java.lang.reflect.Method;
    
    import com.google.gson.Gson;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        Gson gson = new Gson();
    
        // input JSON:
        // {
        //   "dataClass":"com.stackoverflow.q6647866.MyObject",
        //   "method":"myMethod",
        //   "data":"{\"name\":\"fubar\"}"
        // }
        JSONRequest request = gson.fromJson(new FileReader("input.json"), JSONRequest.class);
    
        Class targetClass = Class.forName(request.dataClass);
        System.out.println("targetClass: " + targetClass);
        Object dataObject = gson.fromJson(request.data, targetClass);
    
        Method method = targetClass.getMethod(request.method);
        method.invoke(dataObject);
      }
    }
    
    class JSONRequest
    {
      String method;
      String data;
      String dataClass;
    }
    
    class MyObject
    {
      String name;
      public void myMethod()
      {
        System.out.println("running my method...");
      }
    }
    

    This seems like a very crude way to accomplish the data to object conversion. Is there a better way?

    That’s about as good as it gets, if the target data type and method to invoke are both completely unknown and only provided in the incoming JSON.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6

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.