zoukankan      html  css  js  c++  java
  • 【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);
  • 相关阅读:
    #ASP.NET Core 1.0 Key Features
    #asp.net core mvc 的视图注入
    # asp.net core 1.0 项目结构
    dotnet core 初试两个小问题解决
    1044 火星数字(20 分)
    1043 输出PATest(20 分)
    1042 字符统计(20 分)
    1041 考试座位号(15 分)
    1040 有几个PAT(25 分)
    1039 到底买不买(20 分)
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/9156029.html
Copyright © 2011-2022 走看看