zoukankan      html  css  js  c++  java
  • java 返回json数据

      Student st1 = new Student(1, "dg", 18, new Date());
                Student st2 = new Student(2, "dg", 18, new Date());
                Student st3 = new Student(3, "dg", 18, new Date());
                Student st4 = new Student(4, "dg", 18, new Date());
                Student st5 = new Student(5, "dg", 18, new Date());
                List li = new ArrayList();
                JSONObject JO1 = new JSONObject(st1);
                JSONObject JO2 = new JSONObject(st2);
                JSONObject JO3 = new JSONObject(st3);
                JSONObject JO4 = new JSONObject(st4);
                JSONObject JO5 = new JSONObject(st5);
                li.add(JO1);
                li.add(JO2);
                li.add(JO3);
                li.add(JO4);
                li.add(JO5);
                JSONArray Ja = new JSONArray(li);
                Map ma = new HashMap();
                ma.put("Result", "OK");
                ma.put("Records", Ja);
                JSONObject js = new JSONObject(ma);

                out.print(js);

    返回结果:

    {"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}

    可以将存放Java对象的 列表 直接转化为 json 数组 或对象,不必要过分麻烦的转换。

    从数据库取出后换:

     SourceDao sd = new SourceDao();
     JSONArray ja = new JSONArray(sd.query(content));

  • 相关阅读:
    数据库主键策略
    经历alidns在国外的严重延时
    无题 MVC
    Jquery操作select,左右移动,双击移动 取到所有option的值
    Computop支付网关(一) credit Card
    [转载] 测试的道理
    微信砍价活动总结
    transform,变换
    transition,过渡效果
    文字居底方法
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/6510782.html
Copyright © 2011-2022 走看看