您的位置:首页 > 编程语言 > Java开发

java类的泛型集合转换成json对象

2011-11-22 23:16 381 查看
package
com.sunweb.util.jsonfactory;
02
03
import
java.util.List;
04
05
import
com.sunweb.util.jsonfactory.jsontools.JSONArray;
06
import
com.sunweb.util.jsonfactory.jsontools.JSONException;
07
import
com.sunweb.util.jsonfactory.jsontools.JSONObject;
08
09
/**
10
* @title 公共json数据格式转换
11
* @author 贺彬
12
*/
13
public
class
ConvertJsonUtils {
14
15
public

ConvertJsonUtils() {
16
super
();
17
}
18
19
/**
20
 
* 功能:将泛型集合转换成分页json数据
21
 
*
22
 
* @param list
23
 
*泛型集合
24
 
* @param countList
25
 
*数据集合的总行数
26
 
* @return 分页json数据
27
 
*/
28
public

static
String ConvertListToPageJson(List<?> list,
int
countList) {
29
// 新建一个json数组
30
JSONArray jsonArray =
new
JSONArray();
31
// 新建一个json对象
32
JSONObject jsonObject =
null
;
33
// 遍历泛型集合
34
for

(Object object : list) {
35
jsonObject =
new
JSONObject(object);
36
jsonArray.put(jsonObject);
37
}
38
// 转换数据格式
39
String json = jsonArray.toString();
40
// 拼接字符串
41
StringBuffer sb =
new
StringBuffer();
42
sb.append(
"{\"totalCount\":"
);
43
sb.append(countList);
44
sb.append(
",\"rows\":"
);
45
sb.append(json);
46
sb.append(
"}"
);
47
String jsonString = sb.toString();
48
return

jsonString;
49
}
50
/**
51
 
* 功能:将泛型集合转换成分页json数据
52
 
*
53
 
* @param list
54
 
*泛型集合
55
 
* @param countList
56
 
*数据集合的总行数
57
 
* @return 分页json数据
58
 
*/
59
public

static
String ConvertListToPageJson(List<?> list){
60
// 新建一个json数组
61
JSONArray jsonArray =
new
JSONArray();
62
// 新建一个json对象
63
JSONObject jsonObject =
null
;
64
// 遍历泛型集合
65
for

(Object object : list) {
66
jsonObject =
new
JSONObject(object);
67
jsonArray.put(jsonObject);
68
}
69
// 转换数据格式
70
String json = jsonArray.toString();
71
// 拼接字符串
72
JSONObject jn =
new
JSONObject();
73
try

{
74
jn.put(
"records"
, jsonArray);
75
}
catch
(JSONException e) {
76
// TODO Auto-generated catch block
77
e.printStackTrace();
78
}
79
return

jn.toString();
80
}
81
82
}

[文件] Test.java ~ 739B
下载(22)

01
package
com.sunweb.util.jsonfactory;
02
03
import
java.util.ArrayList;
04
import
java.util.List;
05
06
public
class
Test {
07
08
/**
09
 
* @param args
10
 
*/
11
public

static
void
main(String[] args) {
12
List<Student> list =
new
ArrayList<Student>();
//创建一个泛型的list集合
13
Student stu =
null
;
14
for
(
int

i=
0
;i<
20
;i++){
15
stu =
new
Student();
16
stu.setId(i);
17
stu.setName(
"贺兵"
+i);
18
//模拟有20个对象的list集合
19
list.add(stu);
20
}
21
   
//现在将集合转成json不分页
22
   
String json1 = ConvertJsonUtils.ConvertListToPageJson(list);
23
System.out.println(json1);
24
//分页,总数20条
25
   
String json2 = ConvertJsonUtils.ConvertListToPageJson(list,
20
);
26
System.out.println(json2);
27
}
28
29
}

[文件] Student.java ~ 316B
下载(16)

01
package
com.sunweb.util.jsonfactory;
02
03
public
class
Student {
04
private

int
id;
05
private

String name;
06
public

int
getId() {
07
return

id;
08
}
09
public

void
setId(
int

id) {
10
this
.id = id;
11
}
12
public

String getName() {
13
return

name;
14
}
15
public

void
setName(String name) {
16
this
.name = name;
17
}
18
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: