Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用GSON和@SerializedName获取JSON字段?_Android_Arrays_Json_Gson - Fatal编程技术网

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

Android 如何使用GSON和@SerializedName获取JSON字段?,android,arrays,json,gson,Android,Arrays,Json,Gson,我在解析以下JSON对象时遇到问题 "paymentCurrency": "eur", "paymentOptions": [ { "paymentOptionId": "1CeGuJt2nkxmaMVf", "paymentProfileUpdateNeed

我在解析以下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;
}
公共类匹配详细信息{
//其他领域
@序列化名称(“付款选项”)
公共阵列列表选项;
}
公共类PaymentOptionWrapper{
公共支付选择权;
}
公共类付费选项{
@SerializedName(“paymentOptionId”)
公共字符串paymentOptionId;
@SerializedName(“PaymentProfileUpdateneed”)
公共布尔配置文件updateneed;
@序列化名称(“状态”)
公共字符串状态;
@SerializedName(“supportedCardTypes”)
公共阵列列表支持的卡;
@序列化名称(“类型”)
公共字符串类型;
}
我也尝试过不使用包装器,直接映射列表,但仍然为空。

试试这个

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;
     }
 }
公共类匹配详细信息{
//其他领域
@序列化名称(“付款选项”)
公共阵列列表选项;
公共阵列列表getOptions(){
返回选项;
}
公共无效设置选项(ArrayList选项){
this.options=选项;
}
}
类别付款选项{
@SerializedName(“paymentOptionId”)
公共字符串paymentOptionId;
@SerializedName(“PaymentProfileUpdateneed”)
公共布尔配置文件updateneed;
@序列化名称(“状态”)
公共字符串状态;
@SerializedName(“supportedCardTypes”)
公共阵列列表支持的卡;
@序列化名称(“类型”)
公共字符串类型;
公共字符串getPaymentOptionId(){
返回paymentOptionId;
}
public void setPaymentOptionId(字符串paymentOptionId){
this.paymentOptionId=paymentOptionId;
}
公共布尔值isProfileUpdateNeeded(){
返回profileupdateneed;
}
public void setProfileUpdateEnded(布尔型ProfileUpdateEnded){
this.profileUpdateEnded=profileUpdateEnded;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共阵列列表getSupportedCards(){
返回支持的卡;
}
公共作废设置支持卡(ArrayList支持卡){
this.supportedCards=supportedCards;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
}
试试这个:

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;
    }
}
公共类MatchDetails实现可序列化
{
@序列化名称(“付款选项”)
@暴露
私有列表paymentOptions=null;
私有最终静态长serialVersionUID=7730239716376724487L;
公共列表getPaymentOptions(){
返回付款选项;
}
public void setPaymentOptions(列出paymentOptions){
this.paymentOptions=paymentOptions;
}
}

公共类PaymentOption实现可序列化
{
@SerializedName(“paymentOptionId”)
@暴露
私有字符串paymentOptionId;
@SerializedName(“PaymentProfileUpdateneed”)
@暴露
私有布尔PaymentProfileUpdateEnded;
@序列化名称(“状态”)
@暴露
私有字符串状态;
@SerializedName(“supportedCardTypes”)
@暴露
私有列表支持的CardTypes=null;
@序列化名称(“类型”)
@暴露
私有字符串类型;
私有最终静态长serialVersionUID=-577104877176081166L;
公共字符串getPaymentOptionId(){
返回paymentOptionId;
}
public void setPaymentOptionId(字符串paymentOptionId){
this.paymentOptionId=paymentOptionId;
}
公共布尔值getPaymentProfileUpdateNeeded(){
返回PaymentProfileUpdateneed;
}
public void setPaymentProfileUpdateEnded(布尔PaymentProfileUpdateEnded){
this.paymentProfileUpdateNeeded=paymentProfileUpdateNeeded;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共列表getSupportedCardTypes(){
返回支持的卡片类型;
}
公共作废设置支持的卡片类型(列出支持的卡片类型){
this.supportedCardTypes=supportedCardTypes;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
}

使用此帮助YouTure使用此工具时,生成的代码无效。是否要访问JSON文件末尾提到的paymentOptionId、状态和类型?是。我需要这些信息,JSON文件就像一个配置文件,我需要这些信息来启动流程。我正在使用默认方式,将GsonConverterFactory连接到改装。我没有使用任何自定义解析代码。
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;
    }
}