문제

I am trying to put String[] in jsonObject and getting following error

java.lang.IllegalArgumentException: Invalid type of value. Type: [[Ljava.lang.String;] with value: [[Ljava.lang.String;@189db56] at com.ibm.json.java.JSONObject.put(JSONObject.java:241)

Please help me to resolve this. Thanks

public JSONObject toJSONObject() {

    JSONObject jsonObject = new JSONObject();

    //Use reflection to get a list of all get methods
    //and add there corresponding values to the JSON object
    Class cl = dto.getClass();
    logger.infoFormat("Converting {0} to JSON Object", cl.getName());
    Method[] methods = cl.getDeclaredMethods();

    for (Method method : methods) {
        String methodName = method.getName();
        if (methodName.startsWith("get")) {
            logger.infoFormat("Processing method - {0}", methodName);
            //Check for no parameters
            if (method.getParameterTypes().length == 0) {
                String tag = getLabel(method);
                Object tagValue = new Object();

                try {
                    tagValue = method.invoke(dto);
                } catch (Exception e) {
                    logger.errorFormat("Error invoking method - {0}", method.getName());
                }

                if (method.getReturnType().isAssignableFrom(BaseDTO.class)) {
                    DTOSerializer serializer = new DTOSerializer((BaseDTO) tagValue);
                    jsonObject.put(tag, serializer.toJSONObject());
                } else if (method.getReturnType().isAssignableFrom(List.class)) {
                    ListSerializer serializer = new ListSerializer((List<BaseDTO>) tagValue);                       
                    jsonObject.put(tag, serializer.toJSONArray());                  
                } else {                    
                    if (tagValue != null) jsonObject.put(tag, tagValue);
                }
            }
        }
    }

    return(jsonObject);
}
도움이 되었습니까?

해결책

try

 jsonObject.put("yourKey", Arrays.asList(yorStringArray));

As you should read the manual first http://www.json.org/javadoc/org/json/JSONObject.html there is no variation of it expects an Object[]

다른 팁

Maybe you should take a look at google-gson.

I like it very much to work with json in Java.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top