Gson:我如何解析可以是列表或字符串的多态值?

时间:2017-05-02 01:16:00

标签: java json gson

我需要解析包含很长客户列表的JSON文件。在JSON文件中,每个客户可能有一个id作为字符串:

{
  "cust_id": "87655",
  ...
},

或几个ID作为数组:

   {
      "cust_id": [
        "12345",
        "45678"
      ],
      ...
    },

Customer课程如下:

public class Customer {

    @SerializedName("cust_id")
    @Expose
    private String custId;
    public String getCustId() {
        return custId;
    }

    public void setCustId(String custId) {
        this.custId = custId;
    }
}

我使用Gson解析JSON:

Gson gson = new Gson()
Customers customers1 = gson.fromJson(json, Customers.class)

并且在尝试解析数组时失败并显示com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY

失败的原因很明显。

我的问题:处理这两种情况的最佳方法是什么(当id是一个字符串,当它是一个字符串数组时),给定我无法更改json文件结构

4 个答案:

答案 0 :(得分:2)

如果要处理这两种方案,可以使用自定义反序列化程序。当然,您必须将“cust_id”变量更改为列表或数组。

主:

String json1 = "{\"cust_id\": \"87655\"}";
String json2 = "{\"cust_id\": [\"12345\", \"45678\"]}";

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Customer.class, new CustomerDeserializer());
Gson gson = gsonBuilder.create();

Customer customer1 = gson.fromJson(json1, Customer.class);
System.out.println(customer1);

Customer customer2 = gson.fromJson(json2, Customer.class);
System.out.println(customer2);

客户

public class Customer {

    @SerializedName("cust_id")
    private List<String> custId;

    public List<String> getCustId() {
        return custId;
    }

    public void setCustId(List<String> custId) {
        this.custId = custId;
    }
}

CustomerDeserializer

public class CustomerDeserializer implements JsonDeserializer<Customer> {

@Override
public Customer deserialize(JsonElement jsonElement, Type typeOf, JsonDeserializationContext context) throws JsonParseException {
    Customer result = null;
    Gson gson = new Gson();

    try {
        // try to deserialize by assuming JSON has a list
        result = gson.fromJson(jsonElement, Customer.class);
    } catch (JsonSyntaxException jse) {
        // error here means JSON has a single string instead of a list

        try {
            // get the single ID
            String custId = jsonElement.getAsJsonObject().get("cust_id").getAsString();

            result = new Customer();
            result.setCustId(Arrays.asList(new String[] {custId}));
        } catch (Exception e) {
            // more error handling here
            e.printStackTrace();
        }
    }

    return result;
}

}

输出

Customer [custId=[87655]]
Customer [custId=[12345, 45678]]

答案 1 :(得分:0)

您可以简单地将所有值指定为数组,即使只是一个值。

{
  "cust_id": ["87655"],
  ...
},

更新:如果您无法更改json,则可以绑定除Customer之外的custId类中的每个字段并手动设置。

public class Customer {
    private String[] custId;

    public String getCustId() {
        return custId;
    }

    public void setCustId(String custId) {
        custId = new String[] {custId};
    }

    public void setCustId(String[] custId) {
        this.custId = custId;
    }
}

然后手动解析:

Gson gson = new Gson();
Customers customers = gson.fromJson(json, Customers.class);
Object custId = new JSONObject(json).get("cust_id");
if (custId instanceof String) {
  customers.setCustId((String) custId);
else if (custId instanceof JSONArray) {
  customers.setCustId(convertToStringArray(new JSONArray(custId)));
}

答案 2 :(得分:0)

尝试method overriding

public class Customer {

    @SerializedName("cust_id")
    @Expose
    private String custId;

    public void setCustId(String custId) {
        this.custId = {custId};
    }

    public String[] getCustId() {
        return custId;
    }

    @override
    public void setCustId(String[] custId) {
        this.custId = custId;
    }
}

现在在代码中CUSTID的所有值都是数组而不是字符串

答案 3 :(得分:0)

参考this

现在问题是您必须在返回的地图上编写自己的代码才能获得所需的结果。