首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用GSON和@SerializedName获取JSON字段?

如何使用GSON和@SerializedName获取JSON字段?
EN

Stack Overflow用户
提问于 2018-07-07 18:17:02
回答 2查看 711关注 0票数 0

我在解析以下JSON对象时遇到了问题

代码语言:javascript
复制
                    "paymentCurrency": "eur",
                    "paymentOptions": [
                        {
                            "paymentOptionId": "1CeGuJt2nkxmaMVf",
                            "paymentProfileUpdateNeeded": false,
                            "status": "DISABLED",
                            "supportedCardTypes": [
                                "CARD_TYPE_1",
                                "CARD_TYPE_2",
                                "CARD_TYPE_3"
                            ],
                            "type": "TYPE_1"
                        },
                        {
                            "paymentOptionId": "J8iAFXRZZC07rJdG",
                            "status": "DISABLED",
                            "type": "TYPE_2"
                        }
                    ],
                    "tripCost": "3000",

这就是我到目前为止一直在尝试的。除了@SerializedName和GSON之外,我不能使用任何东西来解析数组。请在下面找到模型类:

代码语言:javascript
复制
public class MatchDetails {
//other fields
   @SerializedName("paymentOptions")
   public ArrayList<PaymentOptionWrapper> options;
}

public class PaymentOptionWrapper {
    public PaymentOption option;
}


public class PaymentOption {

   @SerializedName("paymentOptionId")
   public String paymentOptionId;

   @SerializedName("paymentProfileUpdateNeeded")
   public boolean profileUpdateNeeded;

   @SerializedName("status")
   public String status;

   @SerializedName("supportedCardTypes")
   public ArrayList<String> supportedCards;

   @SerializedName("type")
   public String type;
}

我也尝试过不使用包装器,直接映射列表,但它仍然是空的。

EN

回答 2

Stack Overflow用户

发布于 2018-07-07 18:25:50

试试这个。

代码语言:javascript
复制
public class MatchDetails {
    //other fields
    @SerializedName("paymentOptions")
    public ArrayList<PaymentOption> options;

    public ArrayList<PaymentOption> getOptions() {
        return options;
    }

    public void setOptions(ArrayList<PaymentOption> options) {
        this.options = options;
    }
}


 class PaymentOption {

    @SerializedName("paymentOptionId")
    public String paymentOptionId;

    @SerializedName("paymentProfileUpdateNeeded")
    public boolean profileUpdateNeeded;

    @SerializedName("status")
    public String status;

    @SerializedName("supportedCardTypes")
    public ArrayList<String> supportedCards;

    @SerializedName("type")
    public String type;

     public String getPaymentOptionId() {
         return paymentOptionId;
     }

     public void setPaymentOptionId(String paymentOptionId) {
         this.paymentOptionId = paymentOptionId;
     }

     public boolean isProfileUpdateNeeded() {
         return profileUpdateNeeded;
     }

     public void setProfileUpdateNeeded(boolean profileUpdateNeeded) {
         this.profileUpdateNeeded = profileUpdateNeeded;
     }

     public String getStatus() {
         return status;
     }

     public void setStatus(String status) {
         this.status = status;
     }

     public ArrayList<String> getSupportedCards() {
         return supportedCards;
     }

     public void setSupportedCards(ArrayList<String> supportedCards) {
         this.supportedCards = supportedCards;
     }

     public String getType() {
         return type;
     }

     public void setType(String type) {
         this.type = type;
     }
 }
票数 1
EN

Stack Overflow用户

发布于 2018-07-08 00:32:20

试试这个:

代码语言:javascript
复制
public class MatchDetails implements Serializable
{
    @SerializedName("paymentOptions")
    @Expose
    private List<PaymentOption> paymentOptions = null;

    private final static long serialVersionUID = 7730239716376724487L;

    public List<PaymentOption> getPaymentOptions() {
        return paymentOptions;
    }

    public void setPaymentOptions(List<PaymentOption> paymentOptions) {
        this.paymentOptions = paymentOptions;
    }
}

代码语言:javascript
复制
public class PaymentOption implements Serializable
{
    @SerializedName("paymentOptionId")
    @Expose
    private String paymentOptionId;

    @SerializedName("paymentProfileUpdateNeeded")
    @Expose
    private Boolean paymentProfileUpdateNeeded;

    @SerializedName("status")
    @Expose
    private String status;

    @SerializedName("supportedCardTypes")
    @Expose
    private List<String> supportedCardTypes = null;

    @SerializedName("type")
    @Expose
    private String type;

    private final static long serialVersionUID = -5717104877176081166L;

    public String getPaymentOptionId() {
        return paymentOptionId;
    }

    public void setPaymentOptionId(String paymentOptionId) {
        this.paymentOptionId = paymentOptionId;
    }

    public Boolean getPaymentProfileUpdateNeeded() {
        return paymentProfileUpdateNeeded;
    }

    public void setPaymentProfileUpdateNeeded(Boolean paymentProfileUpdateNeeded) {
        this.paymentProfileUpdateNeeded = paymentProfileUpdateNeeded;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<String> getSupportedCardTypes() {
        return supportedCardTypes;
    }

    public void setSupportedCardTypes(List<String> supportedCardTypes) {
        this.supportedCardTypes = supportedCardTypes;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51222171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档