开发者社区> 问答> 正文

用fastjson把一个带有其他属性的List toJSONString

public class PageList<E> extends ArrayList<E> {
    private static final long serialVersionUID = 1412759446332294208L;
    
    private Paginator paginator;

    public PageList() {}
    
    public PageList(Collection<? extends E> c) {
        super(c);
    }

    
    public PageList(Collection<? extends E> c,Paginator p) {
        super(c);
        this.paginator = p;
    }

    public PageList(Paginator p) {
        this.paginator = p;
    }


    /**
     * 得到分页器,通过Paginator可以得到总页数等值
     * @return
     */
    public Paginator getPaginator() {
        return paginator;
    }

    
}

如上面这个类,在toJSONString 我希望结果是

{
    paginator:{
        "slider": [1, 2, 3, 4, 5, 6, 7],  
        "hasPrePage":false,  
        "startRow": 1,  
        "offset": 0,  
        "lastPage":false,  
        "prePage": 1,  
        "hasNextPage":true,  
        "nextPage": 2,  
        "endRow": 30,  
        "totalCount": 40351,  
        "firstPage":true,  
        "totalPages": 1346,  
        "limit": 30,  
        "page": 1},
"items":[  
        {"NAME":"xiaoma","AGE":30,"GENDER":1,"ID":3,"CITY":"BeiJing"},  
        {"NAME":"xiaoli","AGE":30,"SCORE":85,"GENDER":1,"ID":1,"CITY":"BeiJing"},  
        {"NAME":"xiaowang","AGE":30,"SCORE":92,"GENDER":0,"ID":2,"CITY":"BeiJing"},  
        {"NAME":"xiaoshao","AGE":30,"SCORE":99,"GENDER":0,"ID":4,"CITY":"BeiJing"}  
    ]}

而非

[  
        {"NAME":"xiaoma","AGE":30,"GENDER":1,"ID":3,"CITY":"BeiJing"},  
        {"NAME":"xiaoli","AGE":30,"SCORE":85,"GENDER":1,"ID":1,"CITY":"BeiJing"},  
        {"NAME":"xiaowang","AGE":30,"SCORE":92,"GENDER":0,"ID":2,"CITY":"BeiJing"},  
        {"NAME":"xiaoshao","AGE":30,"SCORE":99,"GENDER":0,"ID":4,"CITY":"BeiJing"}  
    ]

展开
收起
丌冰 2017-08-29 12:27:19 3956 0
1 条回答
写回答
取消 提交回答
  • 很简单, PageList 不要扩展 list 就行了, 而是用 items 属性存 list

    2019-07-17 21:32:36
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载