123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package com.steerinfo.route.util;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import java.math.BigDecimal;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.logging.SimpleFormatter;
- //@Auther Tiroble
- //@meil 2439003195@qq.com
- public class DataConversionTool {
- /**
- *
- * @param json
- * @param type
- * @return
- * @throws Exception
- */
- public static Object jsonToBean(Object json, Object type) throws Exception {
- try{
- JSONObject jsonObject=null;
- //首先需要判断是否是json字符串如果是解析为jsonObject如果是JsonObject就直接赋值给jsonObject
- if (json instanceof String){
- //将json转为JSONOject
- jsonObject= JSONObject.parseObject(json.toString());
- }
- else {
- jsonObject= (JSONObject) json;
- }
- //获得Class对象
- //Class clazz=type.getClass().getDeclaringClass();
- Class clazz= (Class) type;
- //通过空参构造器创建实例
- Constructor constructor = clazz.getDeclaredConstructor();
- constructor.setAccessible(true);
- Object classObject = constructor.newInstance();
- //获得对象的所有属性名
- Field[] fields = clazz.getDeclaredFields();
- //遍历属性集合
- for (int i=0;i<fields.length;i++){
- Field field=fields[i];
- //开启权限
- field.setAccessible(true);
- //判断是否保护属性值
- if(jsonObject.containsKey(field.getName())){
- Object objectValue =jsonObject.get(field.getName());
- //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
- if ((objectValue instanceof Long)||(objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)||(objectValue instanceof BigDecimal)){
- //进行数据判断
- if (field.getType()==BigDecimal.class){
- field.set(classObject,new BigDecimal(objectValue.toString()));
- } else if (field.getType()==Short.class){
- field.set(classObject,Short.parseShort(objectValue.toString()));
- }else if (field.getType()==Date.class){
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date time = formatter.parse(objectValue.toString());
- field.set(classObject,time);
- }
- else {
- if(field.getType()==Long.class||field.getType()==String.class||field.getType()==Integer.class||field.getType()==Boolean.class){
- field.set(classObject,objectValue);
- }
- }
- }
- //集合类型类型
- else if(objectValue instanceof JSONArray){
- Iterator<Object> iterator = ((JSONArray) objectValue).iterator();
- //这里使用的是arryList接收
- List list=new ArrayList<>();
- // 如果是List类型,得到其Generic的类型
- Type genericType = field.getGenericType();
- //如果是空的
- if(genericType == null) {
- genericType=Object.class;
- }
- // 如果是泛型参数的类型
- else if(genericType instanceof ParameterizedType){
- ParameterizedType pt = (ParameterizedType) genericType;
- //得到泛型里的class类型对象
- Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
- genericType=genericClazz;
- }
- while (iterator.hasNext()){
- Object nextObject = iterator.next();
- Object fieldValue = jsonToBean(nextObject, genericType);
- list.add(fieldValue);
- }
- field.set(classObject,list);
- }
- // else if(field.getType()==Object.class){
- // field.set(classObject,objectValue);
- // }
- //如果不是再判断是否是JSONOArray,复杂数据类型
- else{
- //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
- Object fieldValue = jsonToBean(objectValue, field.getType());
- field.set(classObject,fieldValue);
- }
- }
- }
- return classObject;
- }catch (Exception ex){
- throw ex;
- }
- }
- /**
- * 将Object对象里面的属性和值转化成Map对象
- *
- * @param obj
- * @return
- * @throws IllegalAccessException
- */
- public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
- Map<String, Object> map = new HashMap<String,Object>();
- Class<?> clazz = obj.getClass();
- for (Field field : clazz.getDeclaredFields()) {
- field.setAccessible(true);
- String fieldName = field.getName();
- Object value = field.get(obj);
- map.put(fieldName, value);
- }
- return map;
- }
- /**
- *
- * @param map
- * @param type
- * @return
- * @throws Exception
- */
- public static Object mapToBean(Map map, Object type) throws Exception {
- try{
- //获得Class对象
- //Class clazz=type.getClass().getDeclaringClass();
- Class clazz= (Class) type;
- //通过空参构造器创建实例
- Constructor constructor = clazz.getDeclaredConstructor();
- constructor.setAccessible(true);
- Object classObject = constructor.newInstance();
- //获得对象的所有属性名
- Field[] fields = clazz.getDeclaredFields();
- //遍历属性集合
- for (int i=0;i<fields.length;i++){
- Field field=fields[i];
- //开启权限
- field.setAccessible(true);
- //判断是否保护属性值
- if(map.containsKey(field.getName())){
- Object objectValue =map.get(field.getName());
- //判断jsonObject的item是否是String或者Integer,是简单类型直接赋值
- if ((objectValue instanceof String)||(objectValue instanceof Integer)||(objectValue instanceof Boolean)){
- //进行数据判断
- if (field.getType()==BigDecimal.class){
- field.set(classObject,new BigDecimal(objectValue.toString()));
- } else if (field.getType()==Short.class){
- if(objectValue.toString()=="0.00"){
- field.set(classObject,0);
- }
- field.set(classObject,Short.parseShort(objectValue.toString()));
- }else if (field.getType()==Date.class){
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date time = formatter.parse(objectValue.toString());
- field.set(classObject,time);
- }
- else {
- field.set(classObject,objectValue);
- }
- }
- //集合类型类型
- else if(objectValue instanceof List){
- Iterator<Object> iterator = ((List)objectValue).iterator();
- //这里使用的是arryList接收
- List list=new ArrayList<>();
- // 如果是List类型,得到其Generic的类型
- Type genericType = field.getGenericType();
- //如果是空的
- if(genericType == null) {
- genericType=Object.class;
- }
- // 如果是泛型参数的类型
- else if(genericType instanceof ParameterizedType){
- ParameterizedType pt = (ParameterizedType) genericType;
- //得到泛型里的class类型对象
- Class<?> genericClazz = (Class<?>)pt.getActualTypeArguments()[0];
- genericType=genericClazz;
- }
- while (iterator.hasNext()){
- Object nextObject = iterator.next();
- Object fieldValue = jsonToBean(nextObject, genericType);
- list.add(fieldValue);
- }
- field.set(classObject,list);
- }
- //如果不是再判断是否是JSONOArray,复杂数据类型
- else{
- //如果是JSONObject需要判断是否是引用类型,如果是引用类型就还需要将值转为对应类型
- Object fieldValue = jsonToBean(objectValue, field.getType());
- field.set(classObject,fieldValue);
- }
- }
- }
- return classObject;
- }catch (Exception ex){
- throw ex;
- }
- }
- }
|