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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:51:46+00:00 2026-06-04T03:51:46+00:00

I am trying to create a utility class that accepts any type of POJO,

  • 0

I am trying to create a utility class that accepts any type of POJO, and converts that POJO into a JSON object. It should use Java reflections and annotations to look through certain getter methods and create a key, value JSON element based on that.

I am trying to use generics to do this, but it doesn’t seem to work. Is this not possible?

I want to pass a class object as a parameter and retrieve the correct class methods if possible.

Code I have written so far:

package com.jr.freedom.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import net.sf.json.JSONObject;

public class JsonParserUtil {
    private static final String GET_CHAR_SEQUENCE = "get";

    public static <T> JSONObject toJsonObject(Class<T> classObject) {
        // get Method names with @JsonElement included
        Method methods[] = classObject.class.getDeclaredMethods();

        JSONObject jsonObject = new JSONObject();
        try {
            for (int i = 0; i < methods.length; i++) {
                String key = methods[i].getName();
                System.out.println(key);
                if (methods[i].isAnnotationPresent(JsonElement.class) && key.contains(GET_CHAR_SEQUENCE)) {
                    key.replaceFirst(GET_CHAR_SEQUENCE, "");
                    jsonObject.put(key, methods[i].invoke(classObject));
                }
            }
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Here is the testMethod that tries to use the above JSON util object

User user = new User();
user.setBio("bio mate");
user.setCountry("uk");
user.setEmailAddress("jonney@ooglemail.com");
user.setFirstName("jono");
user.setPassword("passwordfdsadsa");
user.setUsername("crazy8");
JSONObject jsonUser =  new JsonParserUtil<User>().toJsonObject(user);

User class is a simple POJO with getters and setters. Currently this line: Method methods[] = T.class.getDeclaredMethods(); won’t work, as you can’t use the generic T to get the DeclaredMethods. Is there a way to do so? Or do I have to create this util method for every single POJO I make?

I know it will work using: Method methods[] = User.class.getDeclaredMethods(); but that will only be applicable for the User class. I am trying to create a util JSON class that can take any POJO object and try to create a JSONObject automatically.

  • 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-06-04T03:51:48+00:00Added an answer on June 4, 2026 at 3:51 am

    Solution:

    public static JSONObject toJsonObject(Object classObject) {
            // get Method names with @JsonElement included
    
            Method methods[] = classObject.getClass().getDeclaredMethods();
    
            JSONObject jsonObject = new JSONObject();
            try {
                for (int i = 0; i < methods.length; i++) {
                    String key = methods[i].getName();
                    System.out.println(key);
                    if (methods[i].isAnnotationPresent(JsonElement.class) && key.startsWith(GET_CHAR_SEQUENCE)) {
                        key = key.replaceFirst(GET_CHAR_SEQUENCE, "");
                        jsonObject.put(key, methods[i].invoke(classObject));
                    }
    
                }
                return jsonObject;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    
        }
    

    The key being to use

    classObject.getClass()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My friend is trying to create a utility function that is given some Type
I'm trying to create a utility class for traversing all the files in a
I am trying to create a utility that doesn't open a window when executed,
I am currently trying to call a method from a utility class that will
I'm trying to write a helper class that represents fields on an object. The
I'm trying to load JSON back into an object. The loads method seems to
In my Lua application, I am trying to create a utility function that sets
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create a utility in C# using the MVC framework where
I am trying to build some utility to create document model from a 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.