Tuesday, January 12, 2016

Convert json string to an array list in java

import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import com.google.gson.Gson;


String json="{}";

Gson gson = new Gson();

Collection<Document> docCollection= null;

List<Document> documentList=null;

docCollection=gson.fromJson(json,new TypeToken<List<Document>>(){}.getType());

if(docCollection instanceof  List){
documentList=(List<Document>)docCollection;

}else{
documentList=new ArrayList<Document>(docCollection);
}