0

为了 -

Config rfqObj = new Gson().fromJson(data, new TypeToken<Config>() {}.getType());

我收到以下异常 -

给定具有根本原因的类型, JsonDeserializer main.java.com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@30de3c87 无法反序列化 json 对象 {} 。main.java.com.google.gson.ParameterizedTypeImpl@7c3d0336]java.lang.IllegalStateException: This is not a JSON Array

JSON数据是 -

{
  "quantities": {
    "142": "0",
    "143": "20",
    "144": "25"
  },
  "characteristics": {},
  "details": {
    "8": "HT Test Report",
    "9": "Others",
    "13": "nTest"
  },
  "materials": {},
  "additionalProcesses": {},
  "suppliers": {}
}

这是 POJO -

public class Config {
Map<Long, String> quantities = new HashMap<Long, String>();
Map<Long, String> characteristics = new HashMap<Long, String>();    
Map<Long, String> details = new HashMap<Long, String>();
Map<Long, String> materials = new HashMap<Long, String>();
Map<Long, String> additionalProcesses = new HashMap<Long, String>();

public Set<Suppliers> suppliers = new HashSet();

//this is for the nested class
public static class Suppliers {
    // defining attributes of this class
    public Long id;
    public String name;
    public Long contactId;
    public String contactInfo;
    public String contactMethod;
    public String contactName;
    public String message;
}
}
4

1 回答 1

0

您收到错误的原因是您的 JSON 文本是具有以下属性的对象:

"suppliers": {}

但是因为您的Config对象希望该属性suppliers是一个集合;Gson 需要一个 JSON 数组,而不是 JSON 对象。

如果你能以某种方式让你的 JSON 文本包含

"suppliers": []

您应该能够将其反序列化为 Java 集。

于 2012-08-05T03:07:46.130 回答