【问题标题】:I have a model class in which when price type is "fixed" it return object and when it is "percentage" it return string, I don't know how to it?我有一个模型类,当价格类型为“固定”时它返回对象,当它是“百分比”时它返回字符串,我不知道怎么做?
【发布时间】:2019-10-30 06:12:21
【问题描述】:

我有一个模型,它收到一个响应,当我有 option 和 furthur 值时,在值中我有一个 priceType,它可以是百分比或固定的。 当 pricetype 是百分比时,响应是 Sting,但是当价格类型固定时,响应 id 对象并且我收到错误..

接受回复

JSONObject jsonResponse = new JSONObject(response);

Gson gson = new GsonBuilder().create();
product = gson.fromJson(String.valueOf(jsonResponse), Product.class);

百分比的 JSON 响应

{
                    "id": 73,
                    "option_id": 21,
                    "price": "7.0000",
                    "price_type": "percent",
                    "position": 0,
                    "created_at": "2019-10-22 04:23:16",
                    "updated_at": "2019-10-30 04:57:45",
                    "label": "Red",
                    "translations": [
                        {
                            "id": 73,
                            "option_value_id": 73,
                            "locale": "en",
                            "label": "Red"
                        }
                    ]
                }

固定价格的 Json 响应

{
                    "id": 74,
                    "option_id": 21,
                    "price": {
                        "amount": "5.0000",
                        "formatted": "$5.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 04:23:16",
                    "updated_at": "2019-10-22 04:23:16",
                    "label": "White",
                    "translations": [
                        {
                            "id": 74,
                            "option_value_id": 74,
                            "locale": "en",
                            "label": "White"
                        }
                    ]
                }

价格可以是类或字符串,但我不知道如何放入我的模型类..

【问题讨论】:

    标签: api android-volley


    【解决方案1】:

    你可以像下面这样声明你的 pojo 类:--

    public class Product<T> {
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getOption_id() {
            return option_id;
        }
    
        public void setOption_id(int option_id) {
            this.option_id = option_id;
        }
    
    
        private int id;
       private int option_id;
    
        public T getPrice() {
            return price;
        }
    
        public void setPrice(T price) {
            this.price = price;
        }
    
        T price;
    
    .............
    }
    

    解析响应后,您可以检查T 的类型,然后按照以下方式提取数据。

    希望它能解决您的问题。如果需要更多代码,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 2019-03-12
      • 2021-11-19
      • 2016-12-05
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      相关资源
      最近更新 更多