zoukankan      html  css  js  c++  java
  • json-lib——JsonConfig详细使用说明

    在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况:

    1、(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了对象A,如果不设置取消环形结构,则那么会抛异常:"There is a cycle in the hierarchy!"

    解决方法:

      在调用JSONObject.fromObject(bean,cfg)时,自定义JsonConfig:

    JsonConfig cfg = new JsonConfig();
    cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

    然后将cfg对象传入fromObject方法中,这样,对象B下面挂的对象A就会被置为NULL。

    2、(Date类型转化)JavaBean出现Date格式时,转化成json时会出现将其转化为:{"date":6,"day":3,"hours":21,"minutes":26,"month":0,"nanos":290000000,"seconds":31,"time":1452086791290,"timezoneOffset":-480,"year":116},这个不易处理,如果需要将Date转换为我们认识的“yyyy-MM-dd”格式,则需要自行创建时间转换器,并实现json-lib中的JsonValueProcessor接口,实现该接口中的两个方法(processArrayValue和processObjectValue):

     1     public class JsonDateValueProcessor implements JsonValueProcessor {  
     2     private String format ="yyyy-MM-dd";  
     3       
     4     public JsonDateValueProcessor() {  
     5         super();  
     6     }  
     7       
     8     public JsonDateValueProcessor(String format) {  
     9         super();  
    10         this.format = format;  
    11     }  
    12   
    13     @Override  
    14     public Object processArrayValue(Object paramObject,  
    15             JsonConfig paramJsonConfig) {  
    16         return process(paramObject);  
    17     }  
    18   
    19     @Override  
    20     public Object processObjectValue(String paramString, Object paramObject,  
    21             JsonConfig paramJsonConfig) {  
    22         return process(paramObject);  
    23     }  
    24       
    25       
    26     private Object process(Object value){  
    27         if(value instanceof Date){    
    28             SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);    
    29             return sdf.format(value);  
    30         }    
    31         return value == null ? "" : value.toString();    
    32     }  
    33   
    34 }  
    JsonDateValueProcessor

      cfg.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());

      这样,对象下面如果有Date类型,转化出来变为“yyyy-MM-dd”格式。

    3、(字段过滤)如果需要在转化过程中去除某些字段,则需要定义一些Excludes字段,具体使用如下:

      String[] EXCLUDES = new String[]{"A","B","C"};

      cfg.setExcludes(EXCLUDES);  

      这样,对象转化时,"A","B","C"会被去除,对象中这些字段转成json时会被删除。

    4、(过滤器PropertyFilter使用)和3有点类似,但是PropertyFilter作用是为了过滤某些符合一些指定条件的属性,如:

    cfg.setJsonPropertyFilter(new PropertyFilter() {
    public boolean apply(Object source, String name, Object value) {
    return value == null;//value为null时返回true,返回true的就是需要过滤调的
    }
    });

    这样,对象转化出来后,生成的 json 字符串只包含非空的值。

    5、其余的常见用法:

    • cfg.setIgnoreDefaultExcludes(true);  //默认为false,即过滤默认的key,改为true则不忽略
    • cfg.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true)); // 忽略掉集合对象 
    • cfg.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, new String[]{"name"})); // 忽略掉name属性及集合对象 
  • 相关阅读:
    使用Project Linker 2012实现多个项目中代码文件
    使用的开放源码Scryber库动态生成PDF文件
    破解Excel 密码保护
    入围51CTO 2009年度“最受读者欢迎的IT图书作者”评选
    谈谈分布式事务之二:基于DTC的分布式事务管理模型[下篇]
    WCF 技术剖析之三十三:你是否了解WCF事务框架体系内部的工作机制?[下篇]
    事件(Event),绝大多数内存泄漏(Memory Leak)的元凶[下篇] (提供Source Code下载)
    如何实现对上下文(Context)数据的统一管理 [提供源代码下载]
    WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于HTTPGET的实现](提供模拟程序)
    Enterprise Library深入解析与灵活应用(9):个人觉得比较严重的关于CachingCallHandler的Bug
  • 原文地址:https://www.cnblogs.com/handsomeye/p/5107398.html
Copyright © 2011-2022 走看看