fastJSON 笔记

前端之家收集整理的这篇文章主要介绍了fastJSON 笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
faseJSON 结果为空时

空数组列表,结果是 [] @H_502_3@ 空对象,结果是:{} @H_502_3@ 前端都可以直接给 js 用。 (感觉有点废话。o( ̄▽ ̄)o )

Java 泛型列表转 JSON
// 按 XXid 查询 hehe 列表@H_403_12@
List<Hehe> heheList = heheService.getHeheByXXid(XXid);
//映射属性名@H_403_12@
NameFilter nameFilter = new@H_403_12@ NameFilter() { 
    public@H_403_12@ String process@H_403_12@(Object source,String name,Object value) {
        if@H_403_12@ (name.equals("heheId"@H_403_12@)) {
            return@H_403_12@ "id"@H_403_12@;
        }else@H_403_12@ if@H_403_12@ (name.equals("heheName"@H_403_12@)) {
            return@H_403_12@ "name"@H_403_12@;
        }
        return@H_403_12@ name;
    }
};

//【转JSON,字段过滤 方法一 】 (以下用的是包含,也可以用排除。包含优先级较高,只要设置,排除就无效了。)@H_403_12@
SimplePropertyPreFilter sppfilter = new@H_403_12@ SimplePropertyPreFilter();
sppfilter.getIncludes().add("heheId"@H_403_12@);
sppfilter.getIncludes().add("heheName"@H_403_12@);
sppfilter.getIncludes().add("heheValue"@H_403_12@);
sppfilter.getIncludes().add("heheRemark"@H_403_12@);
sppfilter.getIncludes().add("sheheImage"@H_403_12@);
//【转JSON,字段过滤 方法二 】,直接写出要序列化的属性 @H_403_12@
//public SimplePropertyPreFilter(Class<?> clazz,String... properties){...}@H_403_12@
sppfilter = new@H_403_12@ SimplePropertyPreFilter(Hehe.class,"heheId"@H_403_12@,"heheName"@H_403_12@,"heheValue"@H_403_12@);
//直接传属性名,它是一个 Class<?> clazz 为 null 的重载。不知其所以然 ( >﹏<) @H_403_12@
//sppfilter = new SimplePropertyPreFilter("heheId","heheName","heheValue");@H_403_12@
JSON.toJSONString(specifications,sppfilter);

//只使用字段过滤(没有映射属性名)@H_403_12@
JSON.toJSONString(specifications,sppfilter,SerializerFeature.UseSingleQuotes);

//【字段过滤】【映射属性名】两个过滤器放进数据,即可同时使用@H_403_12@
//转JSON,支持数组传多个【过滤器】,多个【SerializerFeature】直拉往加后参数就行了,当然也支持传数组(如果多次使用,创建个数组方便些)@H_403_12@
//SerializeFilter[] filterArr = {nameFilter,sppfilter};@H_403_12@
//JSON.toJSONString(specifications,filterArr,SerializerFeature.UseSingleQuotes);@H_403_12@

/** * 如果hehe 有子对象 haha:{ hahaId: 1,heheId: 333,hahaName: "haha老大" } 想避免 haha 里的 heheId 被序列化: * SimplePropertyPreFilter sppfilter = new SimplePropertyPreFilter(SmartSite.class,"siteId","siteNumber","haha","hahaName"); * SimplePropertyPreFilter sppfilter2 = new SimplePropertyPreFilter(SmartSiteConfig.class,"configSortName"); * 转JSON,多个过滤器用数组传参 * SerializeFilter[] filterArr = {nfilter,sppfilter2}; * 【2B注意】子对象的属性,只有在子对象被序列化的前提下,才会出来 */@H_403_12@
fastJSON 序列化时定义属性顺序
//fastJSON序列化时定义属性顺序(所有字段都要列出来,否则无效)@H_403_12@
@JSONType(orders={"heheId"@H_403_12@,"heheValue"@H_403_12@,"heheRemark"@H_403_12@,"sheheImage"@H_403_12@})
public@H_403_12@ class@H_403_12@ heheBean{
    private@H_403_12@ String sheheImage;
    private@H_403_12@ Integer heheId;
    private@H_403_12@ Integer heheValue;
    private@H_403_12@ String heheRemark;
    private@H_403_12@ String heheName;

    //----------- getter setter begin ----------@H_403_12@
    ...
    //----------- getter setter end ----------@H_403_12@
}

参考资料

fastjson SerializerFeature详解 【传送门】 @H_502_3@ Fastjson API SerializeFilte r简介

猜你在找的Json相关文章