Gson: How do I parse polymorphic values that can be either lists or strings?

前端 未结 4 419
心在旅途
心在旅途 2021-01-27 11:57

I need to parse a JSON file that contains long list of customers. In the JSON file each customer may have one id as a string:

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


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 12:20

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

    Now In the code all values of CUSTID will be arrays instead of strings

提交回复
热议问题