flex json

使用flexjson将java对象转换为json字符串。

官方地址:http://flexjson.sourceforge.net/

1.基本用法

            List<SJia> list = SJia.find("region.id = ?", region).fetch();
            Map rtn = new HashMap();
            if (!list.isEmpty()) {
                rtn.put("sJiaList", list);
            }
    
            renderText(new JSONSerializer().include("sJiaList.name","sJiaList.id")
                    .exclude("sJiaList.*").serialize(rtn));

    在上面的代码中,最后的json结果中只包含SJia对象的id和name属性。

    flexjson有以下几个使用中需要注意的地方:

    • 对象的集合属性,默认是不包含在最终的json中的。
    • 在JSONSerializer对象上连续调用include和exclude方法时,先调用的方法优先级较高。

    猜你喜欢

    转载自chenchi1985.iteye.com/blog/1564386