Object转JSON,子类读父类内容
来源:互联网 发布:网络dns错误是什么原因 编辑:程序博客网 时间:2024/06/01 16:04
public class ObjectUtil {/** * 从父类对类中读数据 * @param parent * @param subObj */@SuppressWarnings({ "rawtypes", "unchecked" })public static void readDataFromParent(Object parent, Object subObj){Class parrentClazz = parent.getClass();Method[] methods = parrentClazz.getMethods();try{for(Method method : methods){if(method.getName().startsWith("get") && !method.getName().startsWith("getClass")){Object value = method.invoke(parent);String setName = method.getName().replaceFirst("g", "s");Method subSetMethod = parrentClazz.getMethod(setName, method.getReturnType());subSetMethod.invoke(subObj, value);}}}catch(Exception e){e.printStackTrace();}}@SuppressWarnings({ "rawtypes" })public static JSONObject obj2Json(Object obj){if(obj == null){return null;}Class parrentClazz = obj.getClass();JSONObject ret = new JSONObject();Method[] methods = parrentClazz.getMethods();try{for(Method method : methods){if(method.getName().startsWith("get") && !method.getName().startsWith("getClass")){Object value = method.invoke(obj);if(value == null){continue;}String attrName = method.getName().substring("get".length());attrName = StringUtil.firstCharLower(attrName);if(isBasicType(value)){ret.put(attrName, value);}else if(value instanceof List){List list = (List)value;if(list != null){JSONArray ja = new JSONArray();for(Object varObj : list){if(!isBasicType(varObj)){ja.add(obj2Json(varObj));}else{ja.add(varObj);}}ret.put(attrName, ja);}}else{ret.put(attrName, obj2Json(value));}}}}catch(Exception e){e.printStackTrace();}return ret;}/** * 判断是否是基础类型对象,我把String也当作了基础类型 * @param value * @return */private static boolean isBasicType(Object value){if(value.getClass() == int.class || value instanceof Integer){return true;}else if(value.getClass() == long.class || value instanceof Long){return true;}else if(value.getClass() == short.class || value instanceof Short){return true;}else if(value.getClass() == byte.class || value instanceof Byte){return true;}else if(value.getClass() == boolean.class || value instanceof Boolean){return true;}else if(value.getClass() == double.class || value instanceof Double){return true;}else if(value.getClass() == float.class || value instanceof Float){return true;}else if(value.getClass() == char.class || value instanceof Character){return true;}else if(value instanceof String){return true;}return false;}}
阅读全文
0 0
- Object转JSON,子类读父类内容
- JSON(JavaScript Object Notation)内容详解(Android操作JSON)
- JSON(JavaScript Object Notation)内容详解(Android操作JSON)
- java object 转 json
- Object转json ,xml
- C# json 转 object
- json 转 object
- Android Object 转 Json
- Django object转json
- JSON转object
- IOS json转对象 /json 转 Object / ios json to Object / ios json convert Object
- json type string 转 js json object.
- json转java object list
- object转json 少参数
- .net 二维 json 转 Object
- android Object 转 json格式
- IOS json 转 Object / ios json to Object / ios json convert Object
- IOS json 转 Object / ios json to Object / ios json convert Object
- libcurl库 ftp上传文件到服务器
- Mysql多表查询效率的研究(一)
- Lintcode 二叉树的层次遍历 II
- Greenplum定期vacuum系统表以及定期删除日志
- 《并发编程》--7.声明线程组
- Object转JSON,子类读父类内容
- 十三章 分组数据 GROUP BY ,HAVING
- 从 AngularJS 模块定义说起
- 网络营销实战课-笔记2
- ZOJ3508 The War 贪心,最大流
- Xmind基础教程-保存到印象笔记
- iOS将状态栏设置成白色
- linux内核设计与实现---定时器和时间管理(11)
- 使用一个定时器和回调函数防止代码阻塞