0

我正在尝试将 String[] 放入 jsonObject 并出现以下错误

java.lang.IllegalArgumentException:值的类型无效。类型:[[Ljava.lang.String;] 值:[[Ljava.lang.String;@189db56] at com.ibm.json.java.JSONObject.put(JSONObject.java:241)

请帮我解决这个问题。谢谢

公共 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);
}
4

2 回答 2

2

尝试

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

正如您应该首先阅读手册http://www.json.org/javadoc/org/json/JSONObject.html它没有变化Object[]

于 2012-02-24T11:15:14.900 回答
0

也许你应该看看google-gson

我非常喜欢在 Java 中使用 json。

于 2012-02-24T11:26:28.133 回答