Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-反射-转换为JSON数组_Java_Json - Fatal编程技术网

Java-反射-转换为JSON数组

Java-反射-转换为JSON数组,java,json,Java,Json,我在java中创建和设置如下值 public Class creatObjectWithDefaultValue(String className) throws IllegalArgumentException, IllegalAccessException { DefaultParamValues defaultParamValues = null; Class objectClass = null; try { obj

我在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;
    }
请帮我改正这个错误


非常感谢

JSONObject中没有参数类的构造函数

但您可以创建该类的对象并将其传递给jsonobject构造函数:

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

这只适用于具有无参数构造函数的类

在JSONObject中没有具有参数类的构造函数

但您可以创建该类的对象并将其传递给jsonobject构造函数:

System.out.println ( new JSONObject(te.creatObjectWithDefaultValue("com.hexgen.ro.request.CreateRequisitionRO").newInstance() ).toString () );
这只适用于具有无参数构造函数的类,这一点非常清楚

The constructor JSONObject(Class) is undefined
API→

这一点非常清楚

The constructor JSONObject(Class) is undefined
API→

为什么不使用JSON数据?重新发明轮子通常容易出错,并且只适用于教育或没有任何库或框架适合您的目的的情况

现在,对于JSON对象的转换,没有一个理由不使用更高级别的框架,除非您明确尝试调查反射本身。

为什么不使用一个用于处理JSON数据的框架呢?重新发明轮子通常容易出错,并且只适用于教育或没有任何库或框架适合您的目的的情况


现在,对于JSON Object转换,没有一个理由不使用更高级别的框架,除非您明确尝试调查反射本身。

了解Philipp如何将类转换为JSON对象或JSON数组了解Philipp如何将类转换为JSON对象或JSON数组