Json解析ParsreTools.zip

所需积分/C币:16 2020-04-29 11:03:49 1KB ZIP
6
收藏 收藏
举报

一个SON解析封装类, public class ParsreTools { public static <T> T pasrsJsonStrSimpleT(String jsonString, String key, Class<T> cls) { T t = null; Field fields[] = cls.getDeclaredFields(); String varName[] = new String[fields.length]; for (int i = 0; i < fields.length; i++) { varName[i] = fields[i].getName(); } try { JSONObject jsonObject = new JSONObject(jsonString); JSONObject jsonObject2 = jsonObject.getJSONObject(key); t = cls.newInstance(); for (int i = 0; i < varName.length; i++) { Field field = cls.getDeclaredField(varName[i]); String type = field.getGenericType().toString(); Object object = new Object(); if (type.equals("int")) { object = jsonObject2.getInt(varName[i]); } else if (type.equals("double")) { object = jsonObject2.getDouble(varName[i]); } else if (type.equals("long")) { object = jsonObject2.getLong(varName[i]); } else if (type.equals("boolean")) { object = jsonObject2.getBoolean(varName[i]); } else if (type.equals("class java.lang.String")) { object = jsonObject2.getString(varName[i]); } else if (type.contains("java.util.List")) { int index1 = type.indexOf("<"); int index2 = type.indexOf(">"); type = type.substring(index1 + 1, index2); Class cls1 = Class.forName(type); String jString = jsonObject2.toString(); String key1 = varName[i]; object = pasrsJsonStrMultT(jString, key1, cls1); } else { type = type.replace("class ", ""); Class cls1 = Class.forName(type); String key1 = varName[i]; String jsString = jsonObject2.toString(); object = pasrsJsonStrSimpleT(jsString, key1, cls1); } field.setAccessible(true); field.set(t, object); } } catch (Exception e) { e.printStackTrace(); } return t; } public static <T> List<T> pasrsJsonStrMultT(String jsonString, String key, Class<T> cls) { List<T> list = new ArrayList<T>(); T t = null; Field fields[] = cls.getDeclaredFields();

...展开详情
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
  • GitHub

    绑定GitHub第三方账户获取
  • 脉脉勋章

    绑定脉脉第三方账户获得
  • 签到达人

    累计签到获取,不积跬步,无以至千里,继续坚持!
  • 分享王者

    成功上传51个资源即可获取
关注 私信 TA的资源
上传资源赚积分or赚钱
    最新推荐