gpt4 book ai didi

Java代码实现Map和Object互转及Map和Json互转

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java代码实现Map和Object互转及Map和Json互转由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

具体代码如所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* 使用org.apache.commons.beanutils进行转换
*/
class A {
public static Object mapToObject(Map<String, Object> 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<String, Object> 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<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null )
return null ;
Map<String, Object> map = new HashMap<String, Object>();
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<String, Object> 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<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null ){
return null ;
}
Map<String, Object> map = new HashMap<String, Object>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible( true );
map.put(field.getName(), field.get(obj));
}
return map;
}
<p>} </p><p>
</p><p>from:http: //www.open-open.com/code/view/1423280939826</p>

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

第一段代码 。

?
1
2
3
4
5
6
Map<String,Object> map = new HashMap<String,Object>();
map.put( "method" , "json" );
map.put( "param" , null );
map.put( "time" , "2015-01-23 10:54:55" );
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(map);

第二段代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void readJson2Map(String json) {
ObjectMapper objectMapper = new ObjectMapper();
try {
//将json字符串转成map结合解析出来,并打印(这里以解析成map为例)
Map<String, Map<String, Object>> maps = objectMapper.readValue(
json, Map. class );
System.out.println(maps.size());
Set<String> key = maps.keySet();
Iterator<String> 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的互转的相关知识,希望对大家有所帮助,如果大家想了解更多资讯敬请关注我网站,谢谢! 。

最后此篇关于Java代码实现Map和Object互转及Map和Json互转的文章就讲到这里了,如果你想了解更多关于Java代码实现Map和Object互转及Map和Json互转的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com