我正在尝试将String[]放入jsonObject中,并得到以下错误
java.lang.IllegalArgumentException:无效类型的值。类型:[Ljava.lang.String;with value:[Ljava.lang.String;@189db56 at Ljava.lang.String;@189db56]
请帮我解决这个问题。谢谢
公众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);
}发布于 2012-02-24 11:15:14
试一试
jsonObject.put("yourKey", Arrays.asList(yorStringArray));正如您应该首先阅读手册http://www.json.org/javadoc/org/json/JSONObject.html一样,它没有任何变化,它需要一个Object[]
发布于 2012-02-24 11:26:28
也许你应该看看谷歌-gson。
我非常喜欢在Java中使用json。
https://stackoverflow.com/questions/9429699
复制相似问题