是否有可能创建一个比json更多项目的模型?

时间:2019-02-12 08:15:12

标签: android json server

我有一个包含13个字段的JSON。但是我在Java中创建了一个具有15个字段的模型。因为我想要一个完整的模型,将来它将在服务器端增加这些字段。这样做是错误的? 我的json是:

{
    "CMBL009001": "010001",
    "NMBL009002": 0,
    "CMBL009003": "",
    "CMBL009004": "",
    "CMBL009005": "",
    "CMBL009006": "",
    "NMBL009007": 0,
    "BMBL009008": 0,
    "NMBL009009": 0,
    "CMBL009010": "ADMIN",
    "CMBL009011": "",
    "NMBL009012": 2,
    "NMBL009013": 1
}

我的班级是:

public class NewUser implements Parcelable {
@SerializedName("BMBL009008")
private Long mBMBL009008;
@SerializedName("CMBL009001")
private String mCMBL009001;
@SerializedName("CMBL009003")
private String mCMBL009003;
@SerializedName("CMBL009004")
private String mCMBL009004;
@SerializedName("CMBL009005")
private String mCMBL009005;
@SerializedName("CMBL009006")
private String mCMBL009006;
@SerializedName("CMBL009010")
private String mCMBL009010;
@SerializedName("CMBL009011")
private String mCMBL009011;
@SerializedName("NMBL009002")
private Long mNMBL009002;
@SerializedName("NMBL009007")
private Long mNMBL009007;
@SerializedName("NMBL009009")
private Long mNMBL009009;
@SerializedName("NMBL009012")
private Long mNMBL009012;
@SerializedName("NMBL009013")
private Long mNMBL009013;

protected NewUser(Parcel in) {
    if (in.readByte() == 0) {
        mBMBL009008 = null;
    } else {
        mBMBL009008 = in.readLong();
    }
    mCMBL009001 = in.readString();
    mCMBL009003 = in.readString();
    mCMBL009004 = in.readString();
    mCMBL009005 = in.readString();
    mCMBL009006 = in.readString();
    mCMBL009010 = in.readString();
    mCMBL009011 = in.readString();
    if (in.readByte() == 0) {
        mNMBL009002 = null;
    } else {
        mNMBL009002 = in.readLong();
    }
    if (in.readByte() == 0) {
        mNMBL009007 = null;
    } else {
        mNMBL009007 = in.readLong();
    }
    if (in.readByte() == 0) {
        mNMBL009009 = null;
    } else {
        mNMBL009009 = in.readLong();
    }
    if (in.readByte() == 0) {
        mNMBL009012 = null;
    } else {
        mNMBL009012 = in.readLong();
    }
    if (in.readByte() == 0) {
        mNMBL009013 = null;
    } else {
        mNMBL009013 = in.readLong();
    }
}

public static final Creator<NewUser> CREATOR = new Creator<NewUser>() {
    @Override
    public NewUser createFromParcel(Parcel in) {
        return new NewUser(in);
    }

    @Override
    public NewUser[] newArray(int size) {
        return new NewUser[size];
    }
};

public Long getBMBL009008() {
    return mBMBL009008;
}

public void setBMBL009008(Long bMBL009008) {
    mBMBL009008 = bMBL009008;
}

public String getCMBL009001() {
    return mCMBL009001;
}

public void setCMBL009001(String cMBL009001) {
    mCMBL009001 = cMBL009001;
}

public String getCMBL009003() {
    return mCMBL009003;
}

public void setCMBL009003(String cMBL009003) {
    mCMBL009003 = cMBL009003;
}

public String getCMBL009004() {
    return mCMBL009004;
}

public void setCMBL009004(String cMBL009004) {
    mCMBL009004 = cMBL009004;
}

public String getCMBL009005() {
    return mCMBL009005;
}

public void setCMBL009005(String cMBL009005) {
    mCMBL009005 = cMBL009005;
}

public String getCMBL009006() {
    return mCMBL009006;
}

public void setCMBL009006(String cMBL009006) {
    mCMBL009006 = cMBL009006;
}

public String getCMBL009010() {
    return mCMBL009010;
}

public void setCMBL009010(String cMBL009010) {
    mCMBL009010 = cMBL009010;
}

public String getCMBL009011() {
    return mCMBL009011;
}

public void setCMBL009011(String cMBL009011) {
    mCMBL009011 = cMBL009011;
}

public Long getNMBL009002() {
    return mNMBL009002;
}

public void setNMBL009002(Long nMBL009002) {
    mNMBL009002 = nMBL009002;
}

public Long getNMBL009007() {
    return mNMBL009007;
}

public void setNMBL009007(Long nMBL009007) {
    mNMBL009007 = nMBL009007;
}

public Long getNMBL009009() {
    return mNMBL009009;
}

public void setNMBL009009(Long nMBL009009) {
    mNMBL009009 = nMBL009009;
}

public Long getNMBL009012() {
    return mNMBL009012;
}

public void setNMBL009012(Long nMBL009012) {
    mNMBL009012 = nMBL009012;
}

public Long getNMBL009013() {
    return mNMBL009013;
}

public void setNMBL009013(Long nMBL009013) {
    mNMBL009013 = nMBL009013;
}


public NewUser(){

}
@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (mBMBL009008 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mBMBL009008);
    }
    dest.writeString(mCMBL009001);
    dest.writeString(mCMBL009003);
    dest.writeString(mCMBL009004);
    dest.writeString(mCMBL009005);
    dest.writeString(mCMBL009006);
    dest.writeString(mCMBL009010);
    dest.writeString(mCMBL009011);
    if (mNMBL009002 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mNMBL009002);
    }
    if (mNMBL009007 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mNMBL009007);
    }
    if (mNMBL009009 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mNMBL009009);
    }
    if (mNMBL009012 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mNMBL009012);
    }
    if (mNMBL009013 == null) {
        dest.writeByte((byte) 0);
    } else {
        dest.writeByte((byte) 1);
        dest.writeLong(mNMBL009013);
    }
}
}

2 个答案:

答案 0 :(得分:2)

是的,您可以在模型中包含额外的字段,但是如果服务器未提供这些字段的值,则这些额外的字段将使用默认值初始化。

答案 1 :(得分:1)

如果使用的是GSON,则必须将字段/成员声明标记为@Expose才能进行解析。使用GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()

添加其他字段,请勿序列化这些字段。