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

时间:2018-07-07 10:17:02

标签: android arrays json gson

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

                    "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之外的任何其他东西来解析数组。请在下面找到模型类:

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

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

2 个答案:

答案 0 :(得分:1)

尝试一下...

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 :(得分:0)

尝试一下:

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

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