简体   繁体   English

Java-反射-转换为JSON数组

[英]Java - Reflection - Convert to JSON Array

i create and set values like the following in java 我在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 现在我想将返回对象转换为JSON or JSON Array我尝试了类似的方法,但是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 最好的祝福

Why don't you use a Jackson for working with JSON data? 您为什么不使用Jackson来处理JSON数据? 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. 现在,对于JSON <->对象转换,没有唯一的理由不使用更高级别的框架,除非您明确尝试研究Reflection本身。

there is no constructor with the parameter class in JSONObject. JSONObject中没有带有参数类的构造函数。

but you could create an object of the class and pass it to the jsonobject constructor: 但是您可以创建该类的对象,并将其传递给jsonobject构造函数:

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

this only works with classes that have a parameterless constructor 这仅适用于具有无参数构造函数的类

This is very clear. 这很清楚。

The constructor JSONObject(Class) is undefined

The API → http://www.json.org/javadoc/org/json/JSONObject.html API→ http://www.json.org/javadoc/org/json/JSONObject.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM