java類的泛型集合轉換成json對象__js

來源:互聯網
上載者:User
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("賀兵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.