Gson gson = new Gson();
//==== one string json to class =========
String str ="{\"key\":\"value\"}";
LogData logItem= gson.fromJson(str ,ClassModel.class);
System.out.println(logItem.getLastDumpTime());
//==== Mutiple string json to class =======
JsonParser jsonParser = new JsonParser();
//List type for mutiple arrayList json string -> list class
Type listType = new TypeToken<ArrayList<ClassModel>>() {}.getType();
JsonArray jsonArray = jsonParser.parse("[str ,str ]").getAsJsonArray();
List<ClassModel> datalist = gson.fromJson(jsonArray, listType);
留言
張貼留言