-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path自定义封装JSON操作的类.txt
43 lines (40 loc) · 1.38 KB
/
自定义封装JSON操作的类.txt
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
package com.util;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonHelper {
//从普通的Bean转换为字符串
public static String getJson(Object o){
JSONObject jo=JSONObject.fromObject(o);
return jo.toString();
}
//从Java的列表转换为字符串
public static String getJson(List list){
JSONArray ja=JSONArray.fromObject(list);
return ja.toString();
}
//从Java对象数组转换为字符串
public static String getJson(Object[] arry){
JSONArray ja=JSONArray.fromObject(arry);
return ja.toString();
}
//从json格式的字符串转换为Map对象
public static Map getObject(String s){
return JSONObject.fromObject(s);
}
//从json格式的字符串转换为List数组
public static List getArray(String s){
return JSONArray.fromObject(s);
}
//从json格式的字符串转换为某个Bean
public static Object getObject(String s,Class cls){
JSONObject jo=JSONObject.fromObject(s);
return JSONObject.toBean(jo, cls);
}
//从json格式的字符串转换为某类对象的数组
public static Object getArray(String s,Class cls){
JSONArray ja=JSONArray.fromObject(s);
return JSONArray.toArray(ja, cls);
}
}