Question: i create and set values like the following in java 

```
public Class creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException {
        DefaultParamValues defaultParamValues = null;
        Class objectClass = null;
        try {
            objectClass = Class.forName(className);
             Field[] fields = objectClass.getClass().getDeclaredFields();

             for(Field f:fields){
                    if(!f.isAccessible()){
                        f.setAccessible(true);
                        Class<?> type = f.getType();

                        if(type.equals(Integer.class)){
                            f.set(objectClass, defaultParamValues.INTEGER);  
                        } else if(type.equals(BigInteger.class)){
                            f.set(objectClass, defaultParamValues.BIGINTEGER);  
                        }else if(type.equals(LocalDate.class)){
                            f.set(objectClass, defaultParamValues.DATE);  
                        }else if(type.equals(Boolean.class)){
                            f.set(objectClass, defaultParamValues.BOOLEAN);  
                        }else if(type.equals(Long.class)){
                            f.set(objectClass, defaultParamValues.LONGVALUE);  
                        }
                        f.setAccessible(false);
                    }
                    //System.out.println(f.get(objectClass));
                }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return objectClass;
    }

```

now i want to convert the return object as `JSON or JSON Array` i tried some thing like this but it throws exception as `The constructor JSONObject(Class) is undefined`

```
System.out.println ( new JSONObject( te.creatObjectWithDefaultValue("com.hexgen.ro.request.CreateRequisitionRO") ).toString () );

```

Please help me to correct the mistake.

Best Regards

Answer: 
Why don't you use a [Jackson](http://jackson.codehaus.org/) for working with JSON data? Reinventing the wheel is most often error-prone and is only suitable for either education or the case when no library or framework fits your purposes at all.

Now, for **JSON <-> Object** conversion, there is not a single reason not to use a higher level framework, unless you're explicitly trying to investigate Reflection itself.
there is no constructor with the parameter class in JSONObject.

but you could create an object of the class and pass it to the jsonobject constructor:

```
System.out.println ( new JSONObject(te.creatObjectWithDefaultValue("com.hexgen.ro.request.CreateRequisitionRO").newInstance() ).toString () );

```

this only works with classes that have a parameterless constructor