Arvina Kori
Arvina Kori

Reputation: 103

How to collect data from nested JSONarray which is in JSONobject?

I am newbie at Android. I want to collect data from Ticketmaster api and want to display in ListView. I am using Volley. JSONview of the api

enter image description here

As shown in the image i want to display data from classifications and venues. But while using in the Model class, it is not showing.

the code in the JSONObjectRequest

enter image description here

Please View these images, and help me. How can i get data from classifications and venues?

                          // Parsing json
                      for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject("_embedded");
                            JSONArray array = obj.getJSONArray("events");
                            JSONObject obj2 = array.getJSONObject(i);
                            JSONArray array1 = obj2.getJSONArray("classifications");
                            JSONObject obj3 = array1.getJSONObject(0);
                            JSONObject obj4 = obj3.getJSONObject("segment");
                            Event event = new Event();
                            event.setName(obj2.getString("name"));
                            event.setClassifications(obj4.getString("name")); // here i want to get data from segment

                            // adding movie to movies array
                            eventList.add(event);

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

Thanks!!!

Upvotes: 0

Views: 608

Answers (4)

niya panchal
niya panchal

Reputation: 91

Try this once....

                for (int i = 0; i < response.length(); i++) {
                try {

                    JSONObject obj = response.getJSONObject("_embedded");
                     Iterator iterator = Obj.keys();

                    //while(iterator.hasNext()){
                    String key = (String) iterator.next();
                    JSONObject issue = Obj.getJSONObject(key);
                    JSONArray array = obj.getJSONArray("events");
                    JSONObject obj2 = array.getJSONObject(i);
                    JSONObject obj = response.getJSONObject("_embedded");
                    JSONArray array1 = obj2.getJSONArray("venues");
                    JSONObject obj3 = array1.getJSONObject(0);
                    JSONObject obj4 = obj3.getJSONObject("city");
                    String key1 = (String) iterator.next();
                    JSONObject issue1 = Obj.getJSONObject(key1);
                    Event event = new Event();
                    event.setName(obj2.getString("name"));


                    // adding movie to movies array
                    eventList.add(event);

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

Upvotes: 0

Abdurakhmon
Abdurakhmon

Reputation: 3070

Try this code to get cities

                  for (int i = 0; i < response.length(); i++) {
                    try {

                        JSONObject obj = response.getJSONObject("_embedded");
                        JSONArray array = obj.getJSONArray("events");
                        JSONObject obj2 = array.getJSONObject(i);
                        JSONObject obj = response.getJSONObject("_embedded");
                        JSONArray array1 = obj2.getJSONArray("venues");
                        JSONObject obj3 = array1.getJSONObject(0);
                        JSONObject obj4 = obj3.getJSONObject("city");
                        Event event = new Event();
                        event.setName(obj2.getString("name"));


                        // adding movie to movies array
                        eventList.add(event);

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

Also I recommend using GSON for parsing JSON

Upvotes: 1

Mohit Madaan
Mohit Madaan

Reputation: 469

You can convert your json data to pojo model class. It will help you to easily get values. here is example of your model pojo class

public class ItemOption {
@SerializedName("embeded")
@Expose
private Embeded embeded;public Embeded getEmbeded() {
    return embeded;
}

public void setEmbeded(Embeded embeded) {
    this.embeded = embeded;
}

public class City {

    @SerializedName("name")
    @Expose
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class Embeded {

    @SerializedName("events")
    @Expose
    private List<Event> events = null;

    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        this.events = events;
    }

}

public class Embeded_ {

    @SerializedName("venue")
    @Expose
    private List<Venue> venue = null;

    public List<Venue> getVenue() {
        return venue;
    }

    public void setVenue(List<Venue> venue) {
        this.venue = venue;
    }

}

public class Event {

    @SerializedName("embeded")
    @Expose
    private Embeded_ embeded;

    public Embeded_ getEmbeded() {
        return embeded;
    }

    public void setEmbeded(Embeded_ embeded) {
        this.embeded = embeded;
    }

}


public class Venue {

    @SerializedName("city")
    @Expose
    private City city;

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

}}

and then

itemOption.getEmbeded().getEvents().get(0).getEmbeded().getVenue().get(0).getCity();

Upvotes: 0

Sarthak Gandhi
Sarthak Gandhi

Reputation: 2170

Please check this out.

JSONObject object = new JSONObject(response.toString());
JSONObject embedded = object.getJSONObject("_embedded");
JSONArray eventsArray = embedded.getJSONArray("events");

for(int i = 0;i<eventsArray.length();i++){
  JSONObject event = eventsArray.getJSONObject(i);
  // Parse data from this event object
}

Hope this helps.

Upvotes: 0

Related Questions