Native Nerd Native Nerd - 2 months ago 11
Android Question

Android JSON parsing arrays with GSON

I've been fighting with GSON for the better part of the day. Well, not the GSON so much as my general non-understanding of lists in Android I think. Basically what is happening is I am receiving 2 arrays in JSON, which will then each be formatted and displayed in a list:

Adventures:
+-------------+
| Adventure 1 |
+-------------+
| Adventure 2 |
+-------------+
Events:
+-------------+
| Event 1 |
+-------------+
| Event 2 |
+-------------+


I'm able to read out individual string just fine, but I'm not getting my head around moving this on to arrays. JSON format I'm recieving is:

{
"events": [
{
"eid": "11111111",
"bid": "aaaaaaaa",
"bname": "Example Business 1",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 1",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"valid": true
},
{
"eid": "22222222",
"bid": "bbbbbbbb",
"bname": "Example Business 2",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "50",
"title": "Example Event 2",
"description": "Example Event Description",
"cat": "Nightlife",
"type": "Comedy",
"subtype": "General",
"valid": true
},
{
"eid": "33333333",
"bid": "cccccccc",
"bname": "Example Business 3",
"start": "3/26/14 @ 6pm",
"end": "3/27/14 @ 2am",
"points": "150",
"title": "Example Event 3",
"description": "Example Event Description",
"cat": "Dining",
"type": "Restraunt",
"subtype": "Chinese",
"valid": true
}
],
"adventures": [
{
"aid": "11111111",
"bid": "aaaaaaaa",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "150",
"title": "Example Adventure 1",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Karaoke",
"steps_comp": "2",
"total_steps": "5",
"valid": true
},
{
"aid": "22222222",
"bid": "bbbbbbbb",
"start": "3/26/14 6pm",
"end": "3/27/14 2am",
"points": "250",
"title": "Example Adventure 2",
"description": "Example Adventure Description",
"cat": "Nightlife",
"type": "Bar",
"subtype": "Neighborhood",
"steps_comp": "0",
"total_steps": "5",
"valid": true
}
]


}

I'm parsing out the JSON with the following (Volley code included):

RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, full_url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String example = response.toString();
Log.e("JSON Response", example);

try {
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(example);
JsonArray jsonArr = jo.getAsJsonArray("events");
//jsonArr.
Gson googleJson = new Gson();
ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);

System.out.println("List size is : "+jsonObjList.size());
System.out.println("List Elements are : "+jsonObjList.toString());

for (int i = 0; i < jsonObjList.size(); i++){
String item = jsonObjList.get(i).toString();
System.out.println("Item " + i + " : " + item);
}

Toast.makeText(getApplicationContext(), "0: "+jsonObjList.get(0),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Toast.makeText(getApplicationContext(), "blah",
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}
);
queue.add(jsObjRequest);


I'm getting each row just fine, but it's just a string so I'm not able to get at the individual elements for each. I'm thinking I might need to do another GSON call inside the for loop to break that one as well, then place inside of a array? This seems messy. I've done a bit of googleing(how I've gotten this far), but I can't seem to put the pieces together to make this one work. Any help would be appreciated.

Answer

First create class for each JsonAarry to hold the vales

public class Events {
    private String eid;
    private String bid;
    private String bname;
    private String start;
    private String end;
    private String points;
    private String title;
    private String description;
    private String cat;
    private String type;
    private String subtype;
    private boolean valid;
    @Override
    public String toString() {
        return "Events [eid=" + eid + ", bid=" + bid + ", bname=" + bname
                + ", start=" + start + ", end=" + end + ", points=" + points
                + ", title=" + title + ", description=" + description
                + ", cat=" + cat + ", type=" + type + ", subtype=" + subtype
                + ", valid=" + valid + "]";
    }

}

and

public class Adventures {
    private String aid;
    private String bid;
    private String start;
    private String end;
    private String points;
    private String title;
    private String description;
    private String cat;
    private String type;
    private String subtype;
    private String steps_comp;
    @Override
    public String toString() {
        return "Adventures [aid=" + aid + ", bid=" + bid + ", start=" + start
                + ", end=" + end + ", points=" + points + ", title=" + title
                + ", description=" + description + ", cat=" + cat + ", type="
                + type + ", subtype=" + subtype + ", steps_comp=" + steps_comp
                + ", total_steps=" + total_steps + ", valid=" + valid + "]";
    }
    private String total_steps;
    private boolean valid;

}

Now create a class that hold the whole response

public class ResponseHolder {
    private ArrayList<Events> events;
    private ArrayList<Adventures> adventures;
    @Override
    public String toString() {
        return "ResponseHolder [events=" + events + ", adventures="
                + adventures + "]";
    }
    public ArrayList<Events> getEvents() {
        return events;
    }
    public ArrayList<Adventures> getAdventures() {
        return adventures;
    }
}

and finally

Gson googleJson = new Gson();
ResponseHolder rh= googleJson.fromJson(jsonArr, ResponseHolder.class);

And get the data like

        for(Events e: rh.getEvents()){
            System.out.println(e.toString());
        }

        for(Adventures e: rh.getAdventures()){
            System.out.println(e.toString());
        }
Comments