public String resolveSetDataResult(String str) { Gson gson = new Gson(); Map<String, DataItem> resultMap = gson.fromJson(str, new TypeToken<HashMap<String, DataItem>>() { }.getType()); if (resultMap != null) { for (Map.Entry<String, DataItem> entry : resultMap.entrySet()) { DataItem it = entry.getValue(); if (it != null) { try { Field f = this.getClass().getDeclaredField(entry.getKey()); try { switch (it.type) { case 0: f.set(this, it.getField(3));
break; case 1: f.setInt(this, Integer.parseInt(it.getField(3))); break; case 2: f.setFloat(this, Float.parseFloat(it.getField(3))); break; } } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (NoSuchFieldException e) { e.printStackTrace(); } } } return gson.toJson(this, TraceLevelInfo.class); } else return null; }
|
备注:field调用设置对象(object)的属性字段必须是public,否则写无效
|
参考:https://blog.csdn.net/u011586504/article/details/80063893