IllegalArgumentException JSON
-
12-11-2019 - |
Pergunta
Estou tentando colocar String[] em jsonObject e recebendo o seguinte erro
o java.lang.IllegalArgumentException:Inválido tipo de valor.Tipo:[[Ljava.lang.Seqüência de caracteres;] com o valor:[[Ljava.lang.Seqüência de caracteres;@189db56] em com.a ibm.json.o java.JSONObject.colocar(JSONObject.java:241)
Por favor me ajude a resolver isso.Obrigado
público 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);
}
Solução
tente
jsonObject.put("yourKey", Arrays.asList(yorStringArray));
Como você deve ler o manual de primeiros http://www.json.org/javadoc/org/json/JSONObject.html não há variação de espera de um Object[]
Outras dicas
Talvez você deveria dar uma olhada no google-gson.
Eu gosto muito de trabalhar com json em Java.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow