[ { "type": "123", "value": 123 }, { "type": "234", "value": 234 }]
ImportJava.lang.reflect.Type;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;ImportCom.google.gson.Gson;ImportCom.google.gson.JsonObject;ImportCom.google.gson.reflect.TypeToken; Public classdatafactory{ Public StaticObject Getinstancebyjson (class<?>Clazz, String JSON) {Object obj=NULL; Gson Gson=NewGson (); Obj=Gson.fromjson (JSON, clazz); returnobj; } /** * @authorI321533 *@paramJSON *@paramClazz *@return */ Public Static<T> list<t> jsontolist (String json, class<t[]>clazz) {Gson Gson=NewGson (); T[] Array=Gson.fromjson (JSON, clazz); returnarrays.aslist (array); } /** * @paramJSON *@paramClazz *@return */ Public Static<T> arraylist<t> jsontoarraylist (String json, class<t>clazz) {Type type=NewTypetoken<arraylist<jsonobject>>() {}.gettype (); ArrayList<JsonObject> jsonobjects =NewGson (). Fromjson (JSON, type); ArrayList<T> arrayList =NewArraylist<>(); for(Jsonobject jsonobject:jsonobjects) {Arraylist.add (NewGson (). Fromjson (Jsonobject, clazz)); } returnarrayList; }}
Gson parsing a pure JSON array