本文实例讲述了Java实现的JSONUtil工具类与用法。分享给大家供大家参考,具体如下:
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
|
import java.util.HashMap;
import java.util.Map;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JSONUtils {
/**
* Bean对象转JSON
*
* @param object
* @param dataFormatString
* @return
*/
public static String beanToJson(Object object, String dataFormatString) {
if (object != null ) {
if (StringUtils.isEmpty(dataFormatString)) {
return JSONObject.toJSONString(object);
}
return JSON.toJSONStringWithDateFormat(object, dataFormatString);
} else {
return null ;
}
}
/**
* Bean对象转JSON
*
* @param object
* @return
*/
public static String beanToJson(Object object) {
if (object != null ) {
return JSON.toJSONString(object);
} else {
return null ;
}
}
/**
* String转JSON字符串
*
* @param key
* @param value
* @return
*/
public static String stringToJsonByFastjson(String key, String value) {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return null ;
}
Map<String, String> map = new HashMap<String, String>();
map.put(key, value);
return beanToJson(map, null );
}
/**
* 将json字符串转换成对象
*
* @param json
* @param clazz
* @return
*/
public static Object jsonToBean(String json, Object clazz) {
if (StringUtils.isEmpty(json) || clazz == null ) {
return null ;
}
return JSON.parseObject(json, clazz.getClass());
}
/**
* json字符串转map
*
* @param json
* @return
*/
@SuppressWarnings ( "unchecked" )
public static Map<String, Object> jsonToMap(String json) {
if (StringUtils.isEmpty(json)) {
return null ;
}
return JSON.parseObject(json, Map. class );
}
}
|
测试:
JSON字符串转map
1
2
3
4
5
|
public static void main(String[] args) {
String jsonStr = "{'userName':'huangbaokang','password':'123456'}" ;
Map<String, Object> map = JSONUtils.jsonToMap(jsonStr);
System.out.println(map.get( "userName" ));
}
|
输出:
huangbaokang
JSON字符串转对象:
新建User类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class User {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this .userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
public static void main(String[] args) {
String jsonStr = "{'userName':'huangbaokang','password':'123456'}" ;
User user = (User) JSONUtils.jsonToBean(jsonStr, new User());
System.out.println( "用户名=" +user.getUserName()+ " 密码=" +user.getPassword());
}
|
测试输出:
输出用户名=huangbaokang 密码=123456
对象转JSON
1
2
3
4
5
6
7
|
public static void main(String[] args) {
User user = new User();
user.setUserName( "huangbaokang" );
user.setPassword( "123456" );
String result = JSONUtils.beanToJson(user);
System.out.println(result);
}
|
测试输出:
{"password":"123456","userName":"huangbaokang"}
在这里工具类应用的jar为阿里巴巴的fastjson,在lib中加入相关jar。
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/huangbaokang/article/details/78042956