java vo转map_Java代码实现Map和Object互转及Map和Json互转

先给大家介绍下map和object互相转换的代码。

具体代码如所示:

/**

* 使用org.apache.commons.beanutils进行转换

*/

class A {

public static Object mapToObject(Map map, Class> beanClass) throws Exception {

if (map == null)

return null;

Object obj = beanClass.newInstance();

org.apache.commons.beanutils.BeanUtils.populate(obj, map);

return obj;

}

public static Map, ?> objectToMap(Object obj) {

if(obj == null)

return null;

return new org.apache.commons.beanutils.BeanMap(obj);

}

}

/**

* 使用Introspector进行转换

*/

class B {

public static Object mapToObject(Map map, Class> beanClass) throws Exception {

if (map == null)

return null;

Object obj = beanClass.newInstance();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

Method setter = property.getWriteMethod();

if (setter != null) {

setter.invoke(obj, map.get(property.getName()));

}

}

return obj;

}

public static Map objectToMap(Object obj) throws Exception {

if(obj == null)

return null;

Map map = new HashMap();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

String key = property.getName();

if (key.compareToIgnoreCase("class") == 0) {

continue;

}

Method getter = property.getReadMethod();

Object value = getter!=null ? getter.invoke(obj) : null;

map.put(key, value);

}

return map;

}

}

/**

* 使用reflect进行转换

*/

class C {

public static Object mapToObject(Map map, Class> beanClass) throws Exception {

if (map == null)

return null;

Object obj = beanClass.newInstance();

Field[] fields = obj.getClass().getDeclaredFields();

for (Field field : fields) {

int mod = field.getModifiers();

if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){

continue;

}

field.setAccessible(true);

field.set(obj, map.get(field.getName()));

}

return obj;

}

public static Map objectToMap(Object obj) throws Exception {

if(obj == null){

return null;

}

Map map = new HashMap();

Field[] declaredFields = obj.getClass().getDeclaredFields();

for (Field field : declaredFields) {

field.setAccessible(true);

map.put(field.getName(), field.get(obj));

}

return map;

}

}

from:http://www.open-open.com/code/view/1423280939826

下面给大家介绍Map和json的互相转换

第一段代码

Map map = new HashMap();

map.put("method","json");

map.put("param",null);

map.put("time","2015-01-23 10:54:55");

ObjectMapper mapper = new ObjectMapper();

mapper.writeValueAsString(map);

第二段代码

public static void readJson2Map(String json) {

ObjectMapper objectMapper = new ObjectMapper();

try {

//将json字符串转成map结合解析出来,并打印(这里以解析成map为例)

Map> maps = objectMapper.readValue(

json, Map.class);

System.out.println(maps.size());

Set key = maps.keySet();

Iterator iter = key.iterator();

while (iter.hasNext()) {

String field = iter.next();

System.out.println(field + ":" + maps.get(field));

}

} catch (JsonParseException e) {

e.printStackTrace();

} catch (JsonMappingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

readJson2Map(json);

以上内容是小编给大家介绍的Java代码实现map和Object互转及Map和json的互转的相关知识,希望对大家有所帮助,如果大家想了解更多资讯敬请关注脚本之家网站,谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值