Object转Map和Map转Object(对象转map,map转对象)
1FindArchiveDto findArchiveDto = new FindArchiveDto();
2findArchiveDto.setContractStatus("降龙");
3findArchiveDto.setCustName("明华");
4findArchiveDto.setDepartmentName("赵⽆极");
fastjson转换⽅法
1//Object转Map
2Map map = JSONObject.JSONString(findArchiveDto), Map.class);
3Map<String,Object> map = JSONObject.JSONString(findArchiveDto));
4//Map转Object
5FindArchiveDto findArchiveDto1 = JSON.JSONString(map), FindArchiveDto.class);
6FindArchiveDto findArchiveDto2 = JSONString(map), FindArchiveDto.class);
jackson的转换
1ObjectMapper mapper = new ObjectMapper();
2//对象转map
3Map m = adValue(mapper.writeValueAsString(findArchiveDto), Map.class);
4//map转对象
5FindArchiveDto dto = adValue(mapper.writeValueAsString(m), FindArchiveDto.class);
还可以⽤org.apachemons.beanutils.BeanMap进⾏转换
Map<String, Object> map = new org.apachemons.beanutils.BeanMap(findArchiveDto);
还可以⽤org.apachemons.beanutils.BeanUtils将map转为对象
BeanUtils.populate(findArchiveDto, map);
1//Object转Map
2public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
3 Map<String, Object> map = new LinkedHashMap<String, Object>();
4 Class<?> clazz = Class();
5 System.out.println(clazz);
6 for (Field field : DeclaredFields()) {
7 field.setAccessible(true);
8 String fieldName = Name();
9 Object value = (obj);
10 if (value == null){
11 value = "";
12 }
13 map.put(fieldName, value);
14 }
15 return map;
16}
1//Map转Object
2public static Object mapToObject(Map<Object, Object> map, Class<?> beanClass) throws Exception {
3 if (map == null)
4 return null;
5 Object obj = wInstance();
6 Field[] fields = Class().getDeclaredFields();
object to7 for (Field field : fields) {
8 int mod = Modifiers();
9 if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
10 continue;
11 }
12 field.setAccessible(true);
13 if (Name())) {
14 field.set(obj, (Name()));
15 }
16 }
17 return obj;
18}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论