I am new to JSON and GSON, and I am taking a JSON feed from an input stream, and put it into an array list of custom objects. The JSON feed contains a single object, which contains an array of more objects. It looks something like this:
{ "container":[
{"item_1":"item one"},
{"item_2":"item two"},
{"item_3":"item three"}
]}
I am currently using a TypeToken with a Map to obtain the container object along with the list of objects as an array list of custom objects. Like so:
InputStreamReader input = new InputStreamReader(connection.getInputStream());
Type listType = new TypeToken<Map<String, ArrayList<Item>>>(){}.getType();
Gson gson = new GsonBuilder().create();
Map<String, ArrayList<Item>> treeMap = gson.fromJson(input, listType);
ArrayList<Item> objects = treeMap.get("container");
input.close();
I would like to know if there is a way to skip the step of creating a Map<String, ArrayList<Item>>, and go directly from the input stream to an ArrayList<Item> using GSON. Just to consolidate my code, creating a map seems like an unnecessary step.