IllegalArgumentException JSON
-
12-11-2019 - |
Pregunta
Estoy intentando poner String[] en jsonObject y obtengo el siguiente error
java.lang.IllegalArgumentException:Tipo de valor no válido.Tipo:[[Ljava.lang.String;] con valor:[Ljava.lang.string;@189db56] en com.ibm.json.java.jsonobject.put (jsonObject.java:241)
Por favor ayúdame a resolver esto.Gracias
objeto JSON público a objeto JSON () {
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);
}
Solución
intentar
jsonObject.put("yourKey", Arrays.asList(yorStringArray));
Como deberías leer el manual primero. http://www.json.org/javadoc/org/json/JSONObject.html no hay variación de se espera un Object[]
Otros consejos
Tal vez debe echar un vistazo a Google-gson .
Me gusta mucho trabajar con JSON en Java.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow