【笔记】Ajax请求,dbUtils查询常用结果集转Json串

【温馨提示】这里我用的是org.json.jar! 急性子的,请忽略测试数据,直接看最后的总结!

【javabean】

package com.athl.bean;
import java.util.Date;
public class Person {
    private String name;
    private int age;
    private String sex;
    private Date birthday;
    省略构造、setter、getter、toSting!
}

【dbUtils常用结果集[BeanHandler]、[BeanListHandler]、[MapHandler]、[MapListHandler

package com.athl.json;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import com.athl.bean.Person;
public class JsonDemo {}
@Test
public void jsonDemo(){
    System.out.println("+++++++++【dbUtils查询结果解析为Json】++++++++++\n");
    //一、模拟用dbUtils查出的数据(new BeanHandler<Person>(Person.class))
    Person p = new Person("张三",23,"男",new Date());//在servlet里是Dao对象调用相应的查询函数,我这里只是测试数据
        /*当bean中有java.util.Date类型的属性时,例如birthday。
        解析为Json时,时间就变成了"Thu Dec 29 19:11:19 GMT+08:00 2016"
        解决方法
        1.避免bean中有Date类型的属性,用String,数据库用vachar;
        2.如果bean中Date类型的属性,要参与运算。
          就写一个自定义函数标签在jsp页面上将字符串"Thu Dec 29 19:11:19 GMT+08:00 2016"转一下。
        JSTL自定义函数标签核心代码奉上:用的时候类似${fn:toLowerCase("acd")}直接${x:DateFormat(xx,xx)}
        /**
        * 将字符串"Thu Dec 29 16:03:31 GMT+08:00 2016" 转换为 字符串"2016-12-29 16:03:31"
        * @param source  : "Thu Dec 29 16:03:31 GMT+08:00 2016"
        * @param pattern : "yyyy-MM-dd HH:mm:ss"等时间格式。注意:sss获得错误的毫秒值!
        * @return String
        \*\/ 
        public String DateFormat(String source,String pattern){
            try {
                return new SimpleDateFormat(pattern).format(new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).parse(source)); 
            } catch (Exception e) {
                try {
                    return new SimpleDateFormat(pattern).format(new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH).parse(source));
                } catch (ParseException e1) {
                    return "0000-00-00 00:00:00";//自定义错误返回提示
                }
            } 
        }
        */
        System.out.println("=1=【BeanHandler】dao返回的数据:\n"+p);
    JSONObject arr = new JSONObject(p);
    //response.getWriter().write(arr.toString());//在servlet里返回给Ajax请求
        System.out.println("=2=解析后得到的Json:\n"+arr);
        System.out.println();


    //二、模拟用dbUtils查出的数据(new BeanListHandler<Person>(Person.class))
    List<Person> list = new ArrayList<Person>();//在servlet里是Dao对象调用相应的查询函数,我这里只是测试数据
        list.add(new Person("张三",23,"男",new Date()));
        list.add(new Person("李四",25,"男",new Date()));
        list.add(new Person("王舞",23,"女",new Date()));
        System.out.println("=1=【BeanListHandler】dao返回的数据:\n"+list);
    JSONArray arrList = new JSONArray(list);
    //response.getWriter().write(arrList.toString());//在servlet里返回给Ajax请求
        System.out.println("=2=解析后得到的Json:\n"+arrList);
        System.out.println();


    //三、模拟用dbUtils查出的数据(new MapHandler<Person>(Person.class))
    Map<String,Object> map =new HashMap<String,Object>();//在servlet里是Dao对象调用相应的查询函数,我这里只是测试数据
        map.put("id", 1);
        map.put("name", "张三");
        map.put("age", 22);
        map.put("tel", "12345678901");
        System.out.println("=1=【MapHandler】dao返回的数据:\n"+map);
    JSONObject arrMap = new JSONObject(map);
    //response.getWriter().write(arrMap.toString());//在servlet里返回给Ajax请求
        System.out.println("=2=解析后得到的Json:\n"+arrMap);
        System.out.println();


    //四、准备好将数据库查出来List<Map<String,Object>>数据解析成Json的容器
    JSONArray arrMapList = new JSONArray();
    //模拟用dbUtils查出的数据(new MapListHandler<String,Object>)
    List<Map<String,Object>> lm = new ArrayList<Map<String,Object>>();//在servlet里是Dao对象调用相应的查询函数,我这里只是测试数据
    Map<String,Object> map1 =new HashMap<String,Object>();
        map1.put("id", 1);
        map1.put("name", "张三");
     map1.put("age", 22);
        map1.put("tel", "12345678901");
        lm.add(map1);
        Map<String,Object> map2 =new HashMap<String,Object>();
        map2.put("id", 2);
        map2.put("name", "李四");
        map2.put("age", 23);
        map2.put("tel", "12345678902");
        lm.add(map2);
    System.out.println("=1=【MapListHandler<String,Object>】Dao返回的数据:\n"+lm);

    for(int i=0;i<lm.size();i++){
        arrMapList.put(i,lm.get(i));//有序排列适合经SQL[order by]查出的数据
        //arrMapList.put(lm.get(i));//可能无序
    }
    //response.getWriter().write(arrMapList.toString());//在servlet里返回给Ajax请求
    System.out.println("=2=解析后得到的Json:\n"+arrMapList);

}

【控制台打印结果如下】

+++++++++【查询结果解析为Json】++++++++++

=1=【BeanHandler】dao返回的数据:
Person [name=张三, age=23, sex=男, birthday=Thu Dec 29 20:05:19 GMT+08:00 2016]
=2=解析后得到的Json:
{"birthday":"Thu Dec 29 20:05:19 GMT+08:00 2016","sex":"男","age":23,"name":"张三"}

=1=【BeanListHandler】dao返回的数据:
[Person [name=张三, age=23, sex=男, birthday=Thu Dec 29 20:05:19 GMT+08:00 2016], Person [name=李四, age=25, sex=男, birthday=Thu Dec 29 20:05:19 GMT+08:00 2016], Person [name=王舞, age=23, sex=女, birthday=Thu Dec 29 20:05:19 GMT+08:00 2016]]
=2=解析后得到的Json:
[{"birthday":"Thu Dec 29 20:05:19 GMT+08:00 2016","sex":"男","age":23,"name":"张三"},{"birthday":"Thu Dec 29 20:05:19 GMT+08:00 2016","sex":"男","age":25,"name":"李四"},{"birthday":"Thu Dec 29 20:05:19 GMT+08:00 2016","sex":"女","age":23,"name":"王舞"}]

=1=【MapHandler】dao返回的数据:
{id=1, tel=12345678901, age=22, name=张三}
=2=解析后得到的Json:
{"id":1,"name":"张三","age":22,"tel":"12345678901"}

=1=【MapListHandler<String,Object>】Dao返回的数据:
[{id=1, tel=12345678901, age=22, name=张三}, {id=2, tel=12345678902, age=23, name=李四}]
=2=解析后得到的Json:
[{"id":1,"name":"张三","age":22,"tel":"12345678901"},{"id":2,"name":"李四","age":23,"tel":"12345678902"}]

【总结】

1.查询一条数据

BeanHandler
    Person p = new XxDao().xxx();或者new XxService().xxx();或者你用的是工厂(全局的变量)
    JSONObject arr = new JSONObject(p);
    response.getWriter().write(arr.toString());

MapHandler      
    Map<String,Object> map =new XxDao().xxx();或者new XxService().xxx();或者你用的是工厂(全局的变量)       
    JSONObject arrMap = new JSONObject(map);                
    response.getWriter().write(arrMap.toString()); 

2.查询多条数据

BeanListHandler             
    List<Person> list = new XxDao().xxx();或者new XxService().xxx();或者你用的是工厂(全局的变量)               
    JSONArray arrList = new JSONArray(list);                
    response.getWriter().write(arrList.toString()); 

MapListHandler              
    List<Map<String,Object>> lm = new XxDao().xxx();或者new XxService().xxx();或者你用的是工厂(全局的变量)             
    JSONArray arrMapList = new JSONArray();             
    for(int i=0;i<lm.size();i++){                   
        arrMapList.put(i,lm.get(i));//有序排列适合经SQL[order by]查出的数据                 
        //arrMapList.put(lm.get(i));//可能无序              
    }               
    response.getWriter().write(arrMapList.toString());

PS:刚学Ajax花了点儿时间整理了一下,如果以后用到其它类型的结果集,再研究吧!现阶段应该够用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值