gson 反序列化json时遇到问题

xxls0lw8  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(205)

Every time I am trying to convert my Json to POJO I am getting this error
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 371 path $.x-business-use-case-usage[0] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) at com.google.gson.Gson.fromJson(Gson.java:932) at com.google.gson.Gso
My json file is

{
  "x-fb-trace-id": [
    "VFJHSFvswhd"
  ],
  "x-fb-request-id": [
    "GVMShgvwjmfmsnvhjgs"
  ],
  "null": [
    "HTTP/1.1 200 OK"
  ],
  "Alt-Svc": [
    "h3-29=\":443\"; ma=3600,h3-27=\":443\"; ma=3600"
  ],
  "Access-Control-Allow-Origin": [
    "*"
  ],
  "x-fb-rlafr": [
    "0"
  ],
  "Connection": [
    "Keep-Alive"
  ],
  "x-fb-rev": [
    "16746598"
  ],
  "Pragma": [
    "no-cache"
  ],
  "Date": [
    "Thu, 29 Jul 2019 13:41:17 GMT"
  ],
  "x-business-use-case-usage": [
    "{\"692950841314120\":[{\"type\":\"ads_management\",\"call_count\":3,\"total_cputime\":1,\"total_time\":5,\"estimated_time_to_regain_access\":0}]}"
  ],
  "Strict-Transport-Security": [
    "dbjkabjbfbkvj"
  ],
  "Cache-Control": [
    "private, no-cache, no-store, must-revalidate"
  ],
  "Vary": [
    "Origin"
  ],
  "Expires": [
    "wkdfbvdksjwfbkjfkdb"
  ],
  "Content-Length": [
    "104"
  ],
  "X-FB-Debug": [
    "hdljwbejkfbkwejbkwjebkwjbfkwjfbejkfeifhwjkfbwejkfwbkjfebkf"
  ],
  "facebook-api-version": [
    "v10.0"
  ],
  "Content-Type": [
    "application/json; charset=UTF-8"
  ]
}

And my POJO Structure is

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
@Setter
public class JsonClass {
    @SerializedName("x-business-use-case-usage")
    private List<UseCase> businessUseCase;
    

}
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Getter
@Setter
public class UseCase {
    @SerializedName("692950841314120")
    private List<Case> c;
    
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Case {
    @SerializedName("type")
    private String type;
    @SerializedName("call_count")
    private Integer callCount;
    @SerializedName("total_cputime")
    private Integer totalCputime;
    @SerializedName("total_time")
    private Integer totalTime;
    @SerializedName("estimated_time_to_regain_access")
    private Integer estimatedTimeToRegainAccess;
}

I tried using this code but I am getting above error at the last line where I am trying to convert the json in POJO using gson.

@SuppressWarnings("deprecation")
        JsonParser parser = new JsonParser();
        Object obj = parser.parse(new FileReader("res.json"));
        JsonObject s = (JsonObject)obj;
        Gson gson = new Gson();
        JsonClass header = (JsonClass) gson.fromJson(s.toString(), JsonClass.class);
92dk7w1h

92dk7w1h1#

"x-business-use-case-usage": [
    "{\"692950841314120\":[{\"type\":\"ads_management\",\"call_count\":3,\"total_cputime\":1,\"total_time\":5,\"estimated_time_to_regain_access\":0}]}"
  ],

在这里,正如您所看到的,x-business-use-case-usage是一个string[],其中包含1个元素--它恰好是另一个经过编码的转义json字符串。
你的pojo,期望它是一个List<UseCase>,所以它不会工作,因为实际的json将Map到List<String>
这是错误的说法,该对象是预期的(实际上是{),但给出了string。您必须将其解析为字符串,然后再次解析为UseCase
要使其在单次运行中工作,您可以尝试编写自定义反序列化器,该反序列化器将删除转义,然后解析嵌套的json。有关如何制作自定义反序列化器的信息,请参阅GSON文档。

相关问题