【发布时间】:2019-04-17 15:51:17
【问题描述】:
我已经阅读了很多关于这个错误的答案,但我没有找到适合我的解决方案,也许类对象不一样
我的错误是:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期为 STRING 但为 BEGIN_OBJECT
所以,我只是想像这样从 JSON 中获取一个对象:
JsonObject mS1SystemStatusJsonObject = parameterEntries.getValue().getAsJsonObject();
MS1SystemStatusResponse mS1SystemStatusResponse = gson.fromJson(mS1SystemStatusJsonObject, MS1SystemStatusResponse.class);
我的 JsonObject 是:
{
"WARNING_CONDITION_LIST":
{
"warningConditionList": {"warningCondition":"BLACK_POSTAL_INK_WF_COND"}
},
"DISABLING_CONDITION_LIST": {"disablingConditionList":""},
"CurrentErrorList":"",
"LockState":"lockspend",
"SystemReadyState":true
}
我的区别 POJO(我没有把 get 和 set 简称为代码):
public class MS1SystemStatusResponse{
@SerializedName("LockState")
String lockState;
@SerializedName("SystemReadyState")
Boolean systemReadyState;
@SerializedName("WARNING_CONDITION_LIST")
WARNING_CONDITION_LIST warningConditionListMy;
@SerializedName("DISABLING_CONDITION_LIST")
DISABLING_CONDITION_LIST disablingConditionListMy;
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
@SerializedName("CurrentErrorList")
CurrentErrorList currentErrorList;
public class WARNING_CONDITION_LIST {
@SerializedName(value = "warningConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WarningConditionList warningConditionList;
public class DISABLING_CONDITION_LIST {
@SerializedName(value = "disablingConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
DisablingConditionList disablingConditionList;
public class CurrentErrorList {
@SerializedName(value = "Error")
private Error error;
public class WARNING_CONDITION_LIST {
@SerializedName(value = "warningConditionList")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WarningConditionList warningConditionList;
public class WarningConditionList {
@SerializedName(value = "warningCondition")
String warningCondition;
这是完整的堆栈跟踪和我之前忘记添加的另一个类:
堆栈跟踪:
java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
com.google.gson.internal.bind.JsonTreeReader.nextString(JsonTreeReader.java:154)
com.pb.marqueo.connect.plus.api.common.model.response.EmptyStringAsNullTypeAdapter.read(EmptyStringAsNullTypeAdapter.java:24)
com.pb.marqueo.connect.plus.api.common.model.response.EmptyStringAsNullTypeAdapter.read(EmptyStringAsNullTypeAdapter.java:10)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
com.google.gson.Gson.fromJson(Gson.java:861)
com.google.gson.Gson.fromJson(Gson.java:926)
com.google.gson.Gson.fromJson(Gson.java:899)
com.pb.marqueo.connect.plus.api.core.service.impl.ConnectPlusServicesImpl.getMS1SystemStatus(ConnectPlusServicesImpl.java:410)
com.pb.marqueo.connect.plus.api.core.facade.impl.ConnectPlusBusinessServicesImpl.startProduction(ConnectPlusBusinessServicesImpl.java:1236)
com.pb.marqueo.connect.plus.rest.services.impl.ConnectPlusControllerImpl.startProd(ConnectPlusControllerImpl.java:280)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.pb.marqueo.common.filter.OriginFilter.doFilter(OriginFilter.java:70)
类适配器:
public class EmptyStringAsNullTypeAdapter extends TypeAdapter<String> {
@Override
public void write(JsonWriter out, String value) throws IOException {
out.value(value);
}
@Override
public String read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
String result = in.nextString();
if ("".equals(result)) {
return null;
}
return result;
}
}
我从 1 周开始转身 :(
【问题讨论】:
-
mS1SystemStatusJsonObject 打印什么?
-
您可以尝试将
"SystemReadyState":true替换为"SystemReadyState":"true"吗? -
不,我不能替换这个,它会影响程序中的很多东西,当WARNING_CONDITION_LIST为空时我没有错误
-
询问异常时,总是发布异常的完整异常堆栈跟踪。
-
您在非字符串属性上使用了名为 EmptyStringAsNullTypeAdapter 的适配器。这可能是原因:它需要一个字符串,但得到一个对象。堆栈跟踪可能会证实这一点。