【FastJson】使用学习

FastJson使用学习

1、json转object、object转json

复制代码
List<CompanyLoopPicture> list = new ArrayList<CompanyLoopPicture>();
CompanyLoopPicture clp = new CompanyLoopPicture();
clp.setId(1);
clp.setCompanyId(100);
clp.setTitle("google");
clp.setPicture("/upload/images/1.jpg");
clp.setLink("http://www.google.com");
clp.setFlag("modify");
list.add(clp);
clp = new CompanyLoopPicture();
clp.setId(2);
clp.setCompanyId(200);
clp.setTitle("intel");
clp.setPicture("/upload/images/2.jpg");
clp.setLink("http://www.intel.com");
clp.setFlag("delete");
list.add(clp);
String json = JSON.toJSONString(list);//object转json
System.out.println(json);
List<CompanyLoopPicture> newList = JSON.parseArray(json, CompanyLoopPicture.class);//json转object
System.out.println(newList);
复制代码

2、忽略字段

 方式一、@JSONField(serialize=false)

@JSONField(serialize=false) 
private int displayOrder;

方式二、transient

private transient int displayOrder;

方式三、PropertyFilter,返回false是去掉的字段

复制代码
PropertyFilter profilter = new PropertyFilter(){  
    @Override  
    public boolean apply(Object object, String name, Object value) {
        if(name.equalsIgnoreCase("displayOrder")) {
            return false;
        }  
        return true;
    }
};
String json = JSON.toJSONString(list, profilter);
复制代码

方式四、SimplePropertyPreFilter,指定要序列化的字段

SimplePropertyPreFilter filter = new SimplePropertyPreFilter(CompanyLoopPicture.class, "id","companyId","link","picture","title");
        String json = JSON.toJSONString(list, filter);

 

posted @   翠微  阅读(217)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示