我正在尝试在jsonObject中放入字符串[]并获取错误

java.lang.illegalargumentException:无效类型的值。类型: [[ljava.lang.string;]有值:[[ljava.lang.string; @ 189db56]在 com.ibm.json.java.jsonobject.put(jsonobject.java:241)

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

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);
}
.

有帮助吗?

解决方案

尝试

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

正如您所应该阅读手册第一个 http://www.json.org/javadoc/org/json/jsonobject.html 它没有它的变化预期了一个属性icoticetagcode

其他提示

也许你应该看看 google-gson

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top