import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;
public class Test implements PropertyFilter,JsonValueProcessor{
private String format ="yyyy-MM-dd HH:mm:ss";
/**
* 忽略的属性名称
*/
private String[] fields;
/**
* 是否忽略集合
*/
private boolean ignoreColl = false;
public String[] getFields() {
return fields;
}
public void setFields(String[] fields) {
this.fields = fields;
}
public boolean isIgnoreColl() {
return ignoreColl;
}
public void setIgnoreColl(boolean ignoreColl) {
this.ignoreColl = ignoreColl;
}
public Test() {
super();
}
public Test(String format, String[] fields, boolean ignoreColl) {
super();
this.format = format;
this.fields = fields;
this.ignoreColl = ignoreColl;
}
@Override
public boolean apply(Object source, String name, Object value) {
Field declaredField = null;
//忽略值为null的属性
if(value == null)
return true;
//剔除自定义属性,获取属性声明类型
if(!"data".equals(name) && "data"!=name && !"totalSize".equals(name) && "totalSize"!=name ){
try {
declaredField = source.getClass().getDeclaredField(name);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
// 忽略集合
if (declaredField != null) {
if(ignoreColl) {
if(declaredField.getType() == Collection.class
|| declaredField.getType() == Set.class) {
return true;
}
}
}
// 忽略设定的属性
if(fields != null && fields.length > 0) {
if(juge(fields,name)) {
return true;
} else {
return false;
}
}
return false;
}
/**
* 过滤忽略的属性
* @param s
* @param s2
* @return
*/
public boolean juge(String[] s,String s2){
boolean b = false;
for(String sl : s){
if(s2.equals(sl)){
b=true;
}
}
return b;
}
@Override
public Object processArrayValue(Object paramObject,
JsonConfig paramJsonConfig) {
return process(paramObject);
}
@Override
public Object processObjectValue(String paramString, Object paramObject,
JsonConfig paramJsonConfig) {
return process(paramObject);
}
private Object process(Object value){
if(value instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
public static void main(String[] args) {
JsonConfig config = new JsonConfig();
//设置集合不转换 ---- name过滤不转换
config.setJsonPropertyFilter(new Test("yyyy-MM-dd",new String[]{"name"}, true));
//设置时间转换
config.registerJsonValueProcessor(Date.class,new Test());
Demo demo = new Demo("1", "name", "age", "test", null);
JSONObject object = JSONObject.fromObject(demo, config);
System.out.println(object);
}
}
分享到:
相关推荐
JSON net.sf.json 依赖的jar包 亲测可用 一次导入所有jar包
在用到net.sf.json的时候有时候会因为缺少依赖库而报错,此时可以试试这个包
NULL 博文链接:https://smartgwt.iteye.com/blog/1252268
通过JSONObject json = ... 进入此方法后会继续调用fromObject(Object, JsonConfig)的重载方法,在此重载方法中会通过instanceOf判断待转换的Object对象是否是枚举、注解等类型,这些特殊类型会有特别的判断方法。
JSONArray.fromObject(map)报错:Could not initialize class net.sf.json.JsonConfig。ireport 需要高于2.1版本的包。于是就找了这些包。最后2.2.2适合
缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig 使用json时候将会用到的一个jar包,发现这个包在网上提供的比较少.缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig ...
import net.sf.json.util.CycleDetectionStrategy; import net.sf.json.util.JSONUtils; public class TestJsonServlet { /** * 从一个JSON 对象字符格式中得到一个java对象 * * @param jsonString * @param ...
import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; public class Jsontest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ...
ezmorph.jar包是在使用json时候将会用到jar包,缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig,给大家提供ezmorph-1.0.6.jar,有需要的赶快下载吧! ezmorph.jar基本简介 EZMorph是一个...
配置文件JsonConfig 是一个简单的配置框架,基于 json 和 .NET Framework 4.0+ 中可用的动态类型入门在您的项目中,添加对 JsonConfig.dll 的引用使用 json 格式的配置将名为“app.json.config”的文件添加到您的...
jsonConfig.setJsonPropertyFilter(fs); JSONArray json = JSONArray.fromObject(rs, jsonConfig); return json.toString(); } 调用util: MyJsonUtil.listToJsonArray(list, new MyFilter("count", "pn", "url",...
json开发所需要的jar包,fastjson-1.2.2.jar,fastjson-1.2.2-sources.jar,json.jar。不用到处找了
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Join;...import net.sf.json.JsonConfig; @Action(cou
允许将程序配置文件放入.json文件,其中默认配置可以作为资源嵌入或放置在(web)应用程序文件夹中。 可以通过动态类型访问配置,无需自定义类或任何其他存根代码。 JsonConfig带来了对配置继承的支持,这意味着一...
JSON Config - 一个Swift 3 JSON配置读取库。读取JSON文件从服务器端配置
java后端返回json给前端,包含对象JsonObject,JsonConfig,JsonArray.
一开始进入公司,做起项目来比较不知所措,现在一个月过去了,...jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray json = JSONArray.fromObject(listPm, jsonConfig); outJSONData(j
JSONConfig:抽象重新配置文件类,交替xml,同时支持json文件
browser-engines.jsonconfig-00000000000.saladict my-ublock-backup.txt桌面my-ublock-mobile.txt安卓tampermonkey-backup.txtuseragent-switcher-preferences.jsonScholarscope_local_data.ssbackupsearch.json....
如果您也是JSON的拥护者,但讨厌JSON标准的限制,使其很难用作配置格式,但仍然不想将JSON放弃给YAML(JSON很好,为什么需要另一个非常难看的标记语言)。 您可以尝试使用该库。 该库专注于很好地解决以下问题: ...