android - 是否可以创建一个项目比 json 多的模型?

标签 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
}

我的类(class)是:

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

最佳答案

是的,你可以在你的模型中有额外的字段,但如果服务器没有为这些字段提供值,那么这些额外的字段将被初始化为默认值。

关于android - 是否可以创建一个项目比 json 多的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54645554/

相关文章:

android - Android 数据库的最佳实践

android - 意向 Pebble 应用商店

android - 从 Android ActionBar 隐藏选项卡

java - 如何在java中提取嵌入在JSON文件中的XML

linux - Centos 7 上的服务器问题在更新后无法启动

java - 如何更新由 openFileOutput 创建的文件

JSON Swift 解包 [""]

javascript - AngularJS : Load defer JSON and Use It between multiple Controllers

server - WAMP中的Web服务器根目录在哪里?

linux - 如何前后同步我的本地存储库和我的实时网站? git