Rahul
Rahul

Reputation: 9

Android parse json array(parse data from string)

Below is the response i am getting from the server,

{
  "section_id": "[24,1,5,2]"
}

and I am using GSON library

public class SectionModel {

        @SerializedName("section_id")
        private String mSectionId;

        public String getmSectionId() {
            return mSectionId;
        }

        public void setmProgramName(String mSectionId) {
            this.mSectionId = mSectionId;
        }
    }

I am able to get the value "[2,18,25,26]" and store it in a String.

Now how am I supposed to get those values from String and store in an Integer arraylist.

Upvotes: 0

Views: 39

Answers (1)

Dentor
Dentor

Reputation: 600

Try this method in your code:

public ArrayList<Integer> returnArrayList(String parsetest){
        ArrayList<Integer> integerArrayList= new ArrayList<>();
        parsetest=parsetest.replace("[", "");
        parsetest=parsetest.replace("]", "");
        String[] list = parsetest.split(",");
        for (String item : list) {
            integerArrayList.add(Integer.valueOf(item));
        }
        return integerArrayList;
    }

Feel free to ask any doubt in the method.

Upvotes: 1

Related Questions