-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUtils.java
More file actions
47 lines (40 loc) · 1.05 KB
/
JsonUtils.java
File metadata and controls
47 lines (40 loc) · 1.05 KB
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
package com.json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.List;
/**
* Json例子。
*/
public class JsonUtils {
/**
* 将对象转化为json文本
* @param object
* @param <T>
* @return
*/
public static <T> String objectToJson(Object object){
//将对象转化为Json数组
JSONObject jsonObject= JSONObject.fromObject(object);
System.out.println(jsonObject.getString("age"));
return jsonObject.toString();
}
/**
* 将List对象序列化为JSON文本
* @param list
* @param <T>
* @return
*/
public static <T> String listToJson(List<T> list) {
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
}
/**
* 将Json对象转换为相关类型的对象
* @param jsonObject
* @param beanClass
* @return
*/
public static Object jsonToObject(JSONObject jsonObject,Class beanClass){
return JSONObject.toBean(jsonObject,beanClass);
}
}