对象的字段以键值对的形式返回

但是,如果双向关联都设置成fetch = FetchType.EAGER,就直接可以转换成JSON数据



package com.hikvision.iri.comm;

import java.util.HashSet;
import java.util.Set;


public class One implements FkCalss{
private static final long serialVersionUID = 6351525096093825835L;
private int age=0;
private Set<Many> manys=new HashSet<Many>(0);

    public int getAge() {
    return age;
    }

    public Set<Many> getManys() {
    return manys;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public void setManys(Set<Many> manys) {
    this.manys = manys;
    }

  
}
//-----------------------------------------------------
package com.hikvision.iri.comm;


public class  Many{
private static final long serialVersionUID = -6351525096093825836L;
private String name;
private One one=new One();

    public String getName() {
    return name;
    }

    public One getOne() {
    return one;
    }

    public void setName(String name) {
    this.name = name;
    }

    public void setOne(One one) {
    this.one = one;
    }


}
//------------------------------------------------------------

package com.hikvision.iri.comm;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;

import com.googlecode.jsonplugin.JSONUtil;



public class A {
/**
* 将对象里面所有字段和值放在Map对象里面(包括外键和子类里面的键值对)
* 知识点:1,反射2,PropertyUtils类的getProperty()3,判断一个字段是否是静态类型的4,获取Set<X> X的字节码
* @author yangGuanLin 2011-10-11 下午08:19:47
* @param obj
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
    public static Map<String, Object> getObjectEntry(Object obj) throws Exception {
Map<String, Object> map = null;
try {
map = new HashMap<String, Object>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
Class fieldType = field.getType();//
// 判断是否是静态的属性
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
Object fieldVal = PropertyUtils.getProperty(obj, fieldName);
Object entryVal = null;
//FkCalss:让Yjwzcbkxxb实现FkCalss(什么都没有)接口
if (fieldVal instanceof FkCalss) {
// 外键实体 private Yjwzcbkxxb yjwzcbkxxb=new Yjwzcbkxxb();将Yjwzcbkxxb里面的字段弄成键值对
Field[] fs = fieldType.getDeclaredFields();// 获取字段
Map<String, Object> m = new HashMap<String, Object>();
for (Field f : fs) {
String fName = f.getName();
if (Modifier.isStatic(f.getModifiers())) {
continue;// 是静态的字段
}
Object fVal = PropertyUtils.getProperty(fieldVal, fName);
if (fVal instanceof Set) {
continue;// 如果字段是Set<>,只计算一层,不然就死循环了
}
m.put(fName, fVal);
}
entryVal = m;
} else
if (fieldVal instanceof Set) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> m = null;
// 子类的集合 private Set<Yjwzzbpcdjb> yjwzzbpcdjbs = new HashSet<Yjwzzbpcdjb>(0);
Class cla = (Class)((ParameterizedType)field.getGenericType()).getActualTypeArguments()[0];// 获取范型的类型这里是Yjwzzbpcdjb
for (Object o : (Set)fieldVal) {
Field[] fs = cla.getDeclaredFields();
m = new HashMap<String, Object>();
for (Field f : fs) {
String fName = f.getName();
if (Modifier.isStatic(f.getModifiers())) {
continue;
}
Object fVal = PropertyUtils.getProperty(o, fName);
if (fVal instanceof Set) {
continue;
}
m.put(fName, fVal);
}
list.add(m);
}
entryVal = list;
} else {
entryVal = fieldVal;

}
map.put(fieldName, entryVal);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}

public static void main(String[] args) throws Exception {
Map<String, Object> map= getObjectEntry(new Many());//{"one":{"age":0},"name":null}
//Map<String, Object> map=getObjectEntry(new One());//{"manys":[],"age":0}
String data=JSONUtil.serialize(map);
System.out.println(data);
}
}

你可能感兴趣的:(对象)