A generic collection of Java classes is converted to a JSON object __js

Source: Internet
Author: User
Tags object object stringbuffer
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 public JSON data format conversion
11 * @author He Bin
12 */
13 public class Convertjsonutils {
14
15 Public Convertjsonutils () {
16 Super ();
17 }
18
19 /**
20 * Function: Convert generic collection to component page JSON data
21st *
22 * @param list
23 * Generic Collection
24 * @param countlist
25 * Total number of rows in the data collection
26 * @return Page-paged JSON data
27 */
28 public static String Convertlisttopagejson (list<?> List, int countlist) {
29 Create a new JSON array
30 Jsonarray Jsonarray = new Jsonarray ();
31 Create a new JSON object
32 Jsonobject jsonobject = null;
33 Traversing a generic collection
34 for (Object object:list) {
35 Jsonobject = new Jsonobject (object);
36 Jsonarray.put (Jsonobject);
37 }
38 converting data formats
39 String json = jsonarray.tostring ();
40 Stitching strings
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 * Function: Convert generic collection to component page JSON data
52 *
53 * @param list
54 * Generic Collection
55 * @param countlist
56 * Total number of rows in the data collection
57 * @return Page-paged JSON data
58 */
59 public static String Convertlisttopagejson (list<?> List) {
60 Create a new JSON array
61 Jsonarray Jsonarray = new Jsonarray ();
62 Create a new JSON object
63 Jsonobject jsonobject = null;
64 Traversing a generic collection
65 for (Object object:list) {
66 Jsonobject = new Jsonobject (object);
67 Jsonarray.put (Jsonobject);
68 }
69 converting data formats
70 String json = jsonarray.tostring ();
71 Stitching strings
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 }
[file] Test.java ~ 739B Download
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> (); Create a generic list collection
13 Student stu = null;
14 for (int i=0;i<20;i++) {
15 Stu = new Student ();
16 Stu.setid (i);
17 Stu.setname ("He Bing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.