IllegalArgumentException JSON.
-
12-11-2019 - |
Domanda
Sto cercando di mettere la stringa [] in jsonobject e ottenere il seguente errore
.java.lang.illegalargumentException: tipo di valore non valido.Genere: [[Ljava.lang.string;] con valore: [[ljava.lang.string; @ 189db56] a com.ibm.json.java.jsonobject.put (jsonobject.java:241)
Aiutami a risolvere questo. Grazie
Pubblico 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); }
Soluzione
Prova
jsonObject.put("yourKey", Arrays.asList(yorStringArray));
.
Come dovresti leggere il manuale prima http://www.json.org/javadoc/org/json/jsonobject.html Non ci sono variazioni di ciò si aspetta un Object[]
Altri suggerimenti
Forse dovresti dare un'occhiata a google-gson .
Mi piace molto lavorare con JSON in Java.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow