gpt4 book ai didi

java - 应为 STRING,但实际为 BEGIN_OBJECT

转载 作者:行者123 更新时间:2023-11-30 05:40:38 26 4
gpt4 key购买 nike

我已经阅读了很多有关此错误的答案,但没有找到适合我的解决方案,也许类对象不相同

我的错误是:

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 周起就转身了:(

最佳答案

好的,从评论中的对话中我了解到您希望能够反序列化这两种情况:

{
"WARNING_CONDITION_LIST": {
"warningConditionList": {"warningCondition":"BLACK_POSTAL_INK_WF_COND"}
},
...
}

{
"WARNING_CONDITION_LIST": ""
...
}

在第一个场景中,您需要对象,而在第二个场景中,您需要 null

我注意到的第一件事是您的 @JsonAdapter 注释位于错误的位置。它实际上应该位于 WARNING_CONDITION_LIST 字段中。像这样的事情:

public class MS1SystemStatusResponse {
// ...
@SerializedName("WARNING_CONDITION_LIST")
@JsonAdapter(EmptyStringAsNullTypeAdapter.class)
WARNING_CONDITION_LIST warningConditionListMy;
// ...
}

但是,仅此并不能解决您的问题。问题出在 EmptyStringAsNullTypeAdapter 中。它期望该字段只是一个字符串,并且当它可以是一个对象时不处理这种情况。为此,我们可以编写以下适配器:

public class EmptyStringAsEmptyWarningConditionlTypeAdapter extends TypeAdapter<WARNING_CONDITION_LIST> {

@Override
public void write(JsonWriter out, WARNING_CONDITION_LIST value) throws IOException {
out.beginObject();
out.name("warningConditionList");
out.value(value.warningConditionList.warningCondition);
out.endObject();
}

@Override
public WARNING_CONDITION_LIST read(JsonReader in) throws IOException {
WARNING_CONDITION_LIST result = new WARNING_CONDITION_LIST();
result.warningConditionList = new WarningConditionList();
result.warningConditionList.warningCondition = "";

if (in.peek() == JsonToken.BEGIN_OBJECT) {
in.beginObject();
in.nextName();
in.beginObject();
in.nextName();
result.warningConditionList.warningCondition = in.nextString();
in.endObject();
in.endObject();
} else {
in.nextString();
return null;
}
return result;
}
}

本质上,它会检查您是否有字符串或对象,并分别返回 null 或对象。

这只适用于我用 json 向您展示的这两种情况,并且应该足以帮助您解决其余问题。

您可能会在使用 CurrentErrorList 时遇到类似的问题。

关于java - 应为 STRING,但实际为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731568/

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