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);
  • 相关阅读:
    .net Application的目录
    (转载).NET中RabbitMQ的使用
    (转载)RabbitMQ消息队列应用
    说说JSON和JSONP
    SQL Server中的事务与锁
    StackExchange.Redis Client(转载)
    正则语法的查询,这是纯转载的,为了方便查询
    Regex的性能问题
    解决json日期格式问题的办法
    BenchmarkDotNet(性能测试)
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/9156029.html
Copyright © 2011-2022 走看看