Skip to content Skip to sidebar Skip to footer

Gson - Putting And Getting Arraylist Of Composite Objects

I have class NameAndPostion in which I am going to store name and position of users. And I have created ArrayList of objects of NameAndPosition. ArrayList Lo

Solution 1:

Here is example of convert to JSON and from JSON using gson.

//Convert to JSON
System.out.println(gson.toJson(employee));

//Convert to java objects
System.out.println(gson.fromJson("{'id':1,'firstName':'Lokesh','lastName':'Gupta','roles':['ADMIN','MANAGER'],'birthDate':'17/06/2014'}"
                            , Employee.class));

Deserialize collection

List<Employee> yourClassList = newGson().fromJson(jsonArray, Employee.class);

For generic Collection.

TypelistType=newTypeToken<ArrayList<YourClass>>() {
                    }.getType();
List<YourClass> yourClassList = newGson().fromJson(jsonArray, listType);

you can also refere below link.

Google Gson - deserialize list<class> object? (generic type)

Post a Comment for "Gson - Putting And Getting Arraylist Of Composite Objects"