Skip to content Skip to sidebar Skip to footer

Choose Only Selected Json Value And Save In Sharedpreferences?

I am Able to Parse JSON Data.Exactly what is the issue is that ,i want to store the Student ID in the SharedPreference without suffix i.e means only the integer is to be stored? I

Solution 1:

The best way to pars a JSON is by using Gson. U can add it to your project in your gradle:

compile'com.google.code.gson:gson:2.2.+'

Then you can easily create your model:

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

publicclassExample{

@SerializedName("StdID")@Exposeprivate String stdID;
@SerializedName("NAME")@Exposeprivate String nAME;
@SerializedName("PHONENO")@Exposeprivate String pHONENO;
@SerializedName("DOB")@Exposeprivate String dOB;
@SerializedName("CLASS")@Exposeprivate Integer cLASS;
@SerializedName("GENDER")@Exposeprivate String gENDER;
@SerializedName("ADDRESS")@Exposeprivate String aDDRESS;
@SerializedName("NATIONALITY")@Exposeprivate String nATIONALITY;
@SerializedName("ENROLLEDYEAR")@Exposeprivate String eNROLLEDYEAR;
@SerializedName("Photo")@Exposeprivate Object photo;
@SerializedName("Cat_ID")@Exposeprivate Integer catID;
@SerializedName("base64")@Exposeprivate Object base64;
@SerializedName("studentDetails")@Exposeprivate StudentDetails studentDetails;
@SerializedName("Marks")@Exposeprivate Object marks;
@SerializedName("stdCategory")@Exposeprivate StdCategory stdCategory;

public String getStdID() {
return stdID;
}

public void setStdID(String stdID) {
this.stdID = stdID;
}

public String getNAME() {
return nAME;
}

public void setNAME(String nAME) {
this.nAME = nAME;
}

public String getPHONENO() {
return pHONENO;
}

public void setPHONENO(String pHONENO) {
this.pHONENO = pHONENO;
}

public String getDOB() {
return dOB;
}

public void setDOB(String dOB) {
this.dOB = dOB;
}

public Integer getCLASS() {
return cLASS;
}

public void setCLASS(Integer cLASS) {
this.cLASS = cLASS;
}

public String getGENDER() {
return gENDER;
}

public void setGENDER(String gENDER) {
this.gENDER = gENDER;
}

public String getADDRESS() {
return aDDRESS;
}

public void setADDRESS(String aDDRESS) {
this.aDDRESS = aDDRESS;
}

public String getNATIONALITY() {
return nATIONALITY;
}

public void setNATIONALITY(String nATIONALITY) {
this.nATIONALITY = nATIONALITY;
}

public String getENROLLEDYEAR() {
return eNROLLEDYEAR;
}

public void setENROLLEDYEAR(String eNROLLEDYEAR) {
this.eNROLLEDYEAR = eNROLLEDYEAR;
}

public Object getPhoto() {
return photo;
}

public void setPhoto(Object photo) {
this.photo = photo;
}

public Integer getCatID() {
return catID;
}

public void setCatID(Integer catID) {
this.catID = catID;
}

public Object getBase64() {
return base64;
}

public void setBase64(Object base64) {
this.base64 = base64;
}

public StudentDetails getStudentDetails() {
return studentDetails;
}

public void setStudentDetails(StudentDetails studentDetails) {
this.studentDetails = studentDetails;
}

public Object getMarks() {
return marks;
}

public void setMarks(Object marks) {
this.marks = marks;
}

public StdCategory getStdCategory() {
return stdCategory;
}

public void setStdCategory(StdCategory stdCategory) {
this.stdCategory = stdCategory;
}

}
-----------------------------------com.example.StdCategory.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

publicclassStdCategory{

@SerializedName("Cat_ID")@Exposeprivate Integer catID;
@SerializedName("Category")@Exposeprivate String category;

public Integer getCatID() {
return catID;
}

public void setCatID(Integer catID) {
this.catID = catID;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

}
-----------------------------------com.example.StudentDetails.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

publicclassStudentDetails{

@SerializedName("StdID")@Exposeprivate Integer stdID;
@SerializedName("GUARDIAN_PHONE_NO")@Exposeprivate String gUARDIANPHONENO;
@SerializedName("MOBILE_NO")@Exposeprivate String mOBILENO;
@SerializedName("First_NAME")@Exposeprivate String firstNAME;
@SerializedName("Last_Name")@Exposeprivate String lastName;
@SerializedName("Relation")@Exposeprivate String relation;
@SerializedName("DOB")@Exposeprivate String dOB;
@SerializedName("Education")@Exposeprivate String education;
@SerializedName("Occupation")@Exposeprivate String occupation;
@SerializedName("Income")@Exposeprivate String income;
@SerializedName("Email")@Exposeprivate String email;
@SerializedName("AddLine1")@Exposeprivate String addLine1;
@SerializedName("AddLine2")@Exposeprivate String addLine2;
@SerializedName("State")@Exposeprivate String state;
@SerializedName("Country")@Exposeprivate String country;

public Integer getStdID() {
return stdID;
}

public void setStdID(Integer stdID) {
this.stdID = stdID;
}

public String getGUARDIANPHONENO() {
return gUARDIANPHONENO;
}

public void setGUARDIANPHONENO(String gUARDIANPHONENO) {
this.gUARDIANPHONENO = gUARDIANPHONENO;
}

public String getMOBILENO() {
return mOBILENO;
}

public void setMOBILENO(String mOBILENO) {
this.mOBILENO = mOBILENO;
}

public String getFirstNAME() {
return firstNAME;
}

public void setFirstNAME(String firstNAME) {
this.firstNAME = firstNAME;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getRelation() {
return relation;
}

public void setRelation(String relation) {
this.relation = relation;
}

public String getDOB() {
return dOB;
}

public void setDOB(String dOB) {
this.dOB = dOB;
}

public String getEducation() {
return education;
}

public void setEducation(String education) {
this.education = education;
}

public String getOccupation() {
return occupation;
}

public void setOccupation(String occupation) {
this.occupation = occupation;
}

public String getIncome() {
return income;
}

public void setIncome(String income) {
this.income = income;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAddLine1() {
return addLine1;
}

public void setAddLine1(String addLine1) {
this.addLine1 = addLine1;
}

public String getAddLine2() {
return addLine2;
}

public void setAddLine2(String addLine2) {
this.addLine2 = addLine2;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

}

After that, just with fromJson() method, you can convert any json to your model and retrieve your stdId from it.

UPDATE:

If you want to get only the number from S001 you can do it this way:

Stringstr = "S001";
str = str.subString(1);

after this, you have your number in str.

Post a Comment for "Choose Only Selected Json Value And Save In Sharedpreferences?"