renderJson()方法返回Record问题

public Page<Record> paginate(int pageNumber, int pageSize,String name,String tel) {
		String sql_begin="SELECT boss.name,boss.telephone,shop_type.type_name,tel,address,shop.is_show,shop.is_del,examination,shop.add_time,examination_time ";
		StringBuffer sql_search=new StringBuffer(" FROM shop LEFT JOIN boss ON b_id = boss.id LEFT JOIN shop_type ON t_id = shop_type.id ");
		if(!StringUtils.isEmpty(tel)){
			sql_search.append(" and shop.tel=");
			sql_search.append(tel);
		}
		
		if(!StringUtils.isEmpty(name)){
			sql_search.append(" and shop.name like '");
			sql_search.append(name);
			sql_search.append("%'");
		}
		sql_search.append(" order by shop.id desc");
		
		return Db.paginate(pageNumber, pageSize, sql_begin, sql_search.toString());
	}

上面是我的service里面的方法

public void index(){
		
		int pageNo=getParaToInt("pageNo",1);//获取要查询的页码
		int limit=getParaToInt("limit",10);//页面数据条数
		String telephone=getPara("tel");//要查询的电话号码
		String userName=getPara("name");//店铺名称
		
		Page<Record> page=shopService.paginate(pageNo, limit,userName,telephone);
		renderJson(page);
	}

上面是我controller里面的代码。

我前台通过ajax请求。jfinal返回给我的json数据是这样的

{
    "list": [
        {
            "columns": {
                "type_name": "餐馆",
                "address": "开发测试2",
                "examination": false,
                "name": "test",
                "is_del": true,
                "telephone": "18628379032",
                "tel": "7851120",
                "add_time": 1481016551000,
                "examination_time": 1481016554000,
                "is_show": true
            },
            "columnValues": [
                "餐馆",
                "开发测试2",
                false,
                "test",
                true,
                "18628379032",
                "7851120",
                1481016551000,
                1481016554000,
                true
            ],
            "columnNames": [
                "type_name",
                "address",
                "examination",
                "name",
                "is_del",
                "telephone",
                "tel",
                "add_time",
                "examination_time",
                "is_show"
            ]
        },
        {
            "columns": {
                "type_name": "超市",
                "address": "开发测试",
                "examination": false,
                "name": "test",
                "is_del": true,
                "telephone": "18628379032",
                "tel": "7851227",
                "add_time": 1480920450000,
                "examination_time": 1480920456000,
                "is_show": true
            },
            "columnValues": [
                "超市",
                "开发测试",
                false,
                "test",
                true,
                "18628379032",
                "7851227",
                1480920450000,
                1480920456000,
                true
            ],
            "columnNames": [
                "type_name",
                "address",
                "examination",
                "name",
                "is_del",
                "telephone",
                "tel",
                "add_time",
                "examination_time",
                "is_show"
            ]
        }
    ],
    "pageNumber": 1,
    "pageSize": 10,
    "totalPage": 1,
    "totalRow": 2,
    "firstPage": true,
    "lastPage": true
}

而和数据库表有映射关系的Page<Model>返回的json是这样的

{
    "list": [
        {
            "name": "dfdsad",
            "id": 113,
            "telephone": "15320436222",
            "addtime": 1480677653662,
            "isdel": 1
        },
        {
            "name": "奥特曼",
            "id": 112,
            "telephone": "18628379584",
            "addtime": 1479110762931,
            "isdel": 1
        },
        {
            "name": "奥特曼",
            "id": 111,
            "telephone": "18636987541",
            "addtime": 1478944304096,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 106,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 102,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 101,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 100,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 99,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 98,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        },
        {
            "name": "test",
            "id": 97,
            "telephone": "18628379032",
            "addtime": 1471065195990,
            "isdel": 1
        }
    ],
    "pageNumber": 1,
    "pageSize": 10,
    "totalPage": 11,
    "totalRow": 104,
    "firstPage": true,
    "lastPage": false
}


希望知道的大神能指点一下,帮我解解惑。

跪谢!!!

评论区

JFinal

2016-12-06 19:37

将 renderJson(page); 这一行改成 renderJson(JFinalJson.getJson().toJson(page)); 即可打完收枪

因为你用的 fastjson 或 jackson 这类工具转换 Record 对象时,这些工具都是依赖于 record 中的 getter 方法,而 record 中有一个 getColumns() 方法,所以会对此方法返回的数据进行 json 转换

而 JFinalJson 可以自动识别 Record 对象,而且不依赖于 getter 方法进行转换

要输就输给追求

2016-12-07 09:45

@JFinal 谢谢波总指点!!!确实是这样

热门反馈

扫码入社