我在反序列化 json 时遇到问题

I am facing problem in deserializing my json

每次我尝试将我的 Json 转换为 POJO 时,我都会收到此错误

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.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

我的json文件是

{
  "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"
  ]
}

而我的 POJO 结构是

@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;
}

我尝试使用此代码,但在尝试使用 gson 转换 POJO 中的 json 的最后一行出现上述错误。

@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);

 "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 是一个包含 1 个元素的 string[] - 它恰好是另一个编码的转义 json 字符串。

你的 pojo,期望它是一个 List<UseCase>,所以它不会工作,因为实际的 json 将映射到 List<String>

这就是错误所说的,该对象是预期的(实际上是 {),但给出了 string。您必须将其解析为字符串,然后再次将其解析为 UseCase

为了使其在单个 运行 中工作,您可以尝试编写自定义反序列化程序,它将删除转义,然后解析嵌套 json。请参阅 GSON 文档,了解如何制作自定义反序列化器。