Geoff
Geoff

Reputation: 6639

Android fetch volley data from a string of arrays

AM sending a request using android volley and get a response like this

   public void onResponse(JSONObject response

       try {
           String responsedata = response.getString("data");

              Log.i("test",responsedata);
            } catch (JSONException e) {
               e.printStackTrace();
           }

     }

The above log prints

[{"id":1,"identifier":"TYam Plant","header_val":"tyamplant"},
 {"id":2,"identifier":"Touron Plant","header_val":"toroun"}
]

Now i would like to loop through these and extract an array of individual properties that is

id, identifier, header_val

How do i go about this. Am still new to java

Upvotes: 0

Views: 1080

Answers (2)

AskNilesh
AskNilesh

Reputation: 69699

You response is JSONArray not JSONObject check it

You need to use JSONArray request instead of JSONObject request of volley

Try this to parse your json

    try {
        JSONArray jsonArray= new JSONArray(responsedata);
        for (int i=0;i<jsonArray.length();i++){
            JSONObject object=jsonArray.getJSONObject(i);
            String id=object.getString("id");
            String identifier=object.getString("identifier");
            String header_val=object.getString("header_val");
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

Better to Parse your JSON Using google-gson-library

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of

EXAMPLE : How to Parse JSON Array with Gson

Upvotes: 4

Tung Tran
Tung Tran

Reputation: 2955

  1. You create a Custom Object with 3 field: id, identifier,header_val

public class YourObject {
        private String id;
        private String identifier;
        private String header_val;

        public YourObject(String id, String identifier, String header_val) {
            this.id = id;
            this.identifier = identifier;
            this.header_val = header_val;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getIdentifier() {
            return identifier;
        }

        public void setIdentifier(String identifier) {
            this.identifier = identifier;
        }

        public String getHeader_val() {
            return header_val;
        }

        public void setHeader_val(String header_val) {
            this.header_val = header_val;
        }
    }

  1. Loop and add into list: List<YourObject> yourList = new ArrayList<>();

    try {
        JSONArray jsonArray= new JSONArray(responseString);
        for (int i=0; i<jsonArray.length();i++){
            JSONObject object=jsonArray.getJSONObject(i);
            String id=object.getString("id");
            String identifier=object.getString("identifier");
            String header_val=object.getString("header_val");
            yourList.add(new YourObject(id, identifier, header_val);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 0

Related Questions