gpt4 book ai didi

java - IllegalArgumentException JSON

转载 作者:行者123 更新时间:2023-12-02 07:57:24 29 4
gpt4 key购买 nike

我试图将 String[] 放入 jsonObject 并收到以下错误

java.lang.IllegalArgumentException: Invalid type of value. Type: [[Ljava.lang.String;] with value: [[Ljava.lang.String;@189db56] at 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它没有任何变化,需要一个 Object[]

关于java - IllegalArgumentException JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9429699/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com