Java – object input method (iteratre through input variables)?

Solution for Java – object input method (iteratre through input variables)?
is Given Below:

I’m trying to write this method, it takes an object passed and I want to iterate through the variables, store the info about it and access the value.

The reason i want to access it this way is to get varied input based on the object constructors and modify output to something either by string or file for other use.

If anyone has an idea because im stumped, within the for loop below VVVV

public static String JsonCompiler(Object inputObject){
        String objectOutput="";

        Random random = new Random();
        int variation = (int) Math.floor(Math.random() * (3 - 1) + 1);


       if(variation == 1){

           Field[] fields = inputObject.getClass().getDeclaredFields();

           for(Field field: fields) {

               //System.out.println(field + " - " + inputObject[field].toString);
               // variable 1 = inputObject.get{} <<^^ somehow get to understand


       else if (variation == 2){

       else {

        return objectOutput;

So with Fields you have to ensure that they are public first, you can check by using field.canAccess(object). If you want the value from the Field you need call the field from the object for example System.out.println(field.get(object)); which feels very awkward but the field is only the variable name from the class, its the object that holds the value.

From there you can set the value as well by using field.set(object, newValue) ensuring that the types are the same otherwise it will throw an exception at you.