//JSON字符串转单个对象 protected T getResult(String JSONString, Class classtype) { if (Tools.isEmpty(JSONString)) return null; T t = null; try { Gson gson = new Gson(); t = gson.fromJson(JSONString, classtype); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, “接收数据解析异常”, Toast.LENGTH_SHORT).show(); } return t; }
//JSON字符串转对象数组 protected <T> List<T> getResultList(String data, Class<T> classtype) { if (Tools.isEmpty(data)) return null; List<T> members = null; try { Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray Jarray = parser.parse(data).getAsJsonArray(); members = new ArrayList<>(); for (JsonElement obj : Jarray) { T member = gson.fromJson(obj, classtype); members.add(member); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "接收数据[数组]解析异常", Toast.LENGTH_SHORT).show(); } return members; } //JSON字符串转Map protected Map getResultMap(String JSONString) { if (Tools.isEmpty(JSONString)) return null; Map t = null; try { Gson gson = new Gson(); t = gson.fromJson(JSONString, Map.class); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "接收数据解析异常", Toast.LENGTH_SHORT).show(); } return t; }