`

JsonConfig

    博客分类:
  • JSON
阅读更多

原文:http://blog.csdn.net/laixiaonian/article/details/5953260

我们通常对一个Json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值,而json-lib包中的JsonConfig为我们提供了这种功能,具体实现方法有以下几种:

(1)实现JSONString接口

(2)建立JsonConfig实例,并配置属性排除列表

(3)用属性过滤器

(4)写一个自定义的 JsonBeanProcessor.

1. 实现JSONString接口的方法

public class Person implements JSONString { 
private String name; 
private String lastname; 
private Address address; 

// getters & setters 

public String toJSONString() {
return "{name:'"+name+"',lastname:'"+lastname+"'}";
}
}


2.第二种方法通过jsonconfig实例,对包含和需要排除的属性进行方便的添加或删除

public class Person { 
private String name; 
private String lastname; 
private Address address; 

// getters & setters 


JsonConfig jsonConfig = new JsonConfig(); 
jsonConfig.setExclusions( new String[]{"address"}); 
Person bean = new Person("jack","li"); 
JSON json = JSONSerializer.toJSON(bean, jsonConfig); 


3. 使用propertyFilter可以允许同时对需要排除的属性和类进行控制,这种控制还可以是双向的,也可以应用到json字符串到java对象

public class Person { 
private String name; 
private String lastname; 
private Address address; 

// getters & setters 


JsonConfig jsonConfig = new JsonConfig(); 
jsonConfig.setJsonPropertyFilter( new PropertyFilter(){ 

public boolean apply(Object source/* 属性的拥有者 */ , String name /*属性名字*/ , Object value/* 属性值 */ ){ 
// return true to skip name 
return source instanceof Person && name.equals("address"); 

}); 
Person bean = new Person("jack","li");
JSON json = JSONSerializer.toJSON( bean, jsonConfig ) 


4. 最后来看JsonBeanProcessor,这种方式和实现JsonString很类似,返回一个代表原来的domain类的合法JSONObject

public class Person { 
private String name; 
private String lastname; 
private Address address; 

// getters & setters 


JsonConfig jsonConfig = new JsonConfig(); 
jsonConfig.registerJsonBeanProcessor( Person.class, new JsonBeanProcessor(){ 

public JSONObject processBean( Object bean, JsonConfig jsonConfig ){ 
if(!(bean instanceof Person)){ 
return new JSONObject(true); 

Person person = (Person) bean; 
return new JSONObject() .element( "name", person.getName()) .element( "lastname", person.getLastname()); 

}); 


Person bean = new Person("jack","li");
JSON json = JSONSerializer.toJSON( bean, jsonConfig );

<!-- Baidu Button BEGIN -->
分享到:
评论

相关推荐

    swift-JSONConfig-一个Swift3JSON配置读取库

    Swift-JSONConfig是一个针对Swift 3的库,专门设计用于简化服务器端JSON配置文件的读取和解析。在iOS、macOS以及其他支持Swift的平台上,开发者经常需要从远程服务器获取配置信息,以便根据不同的环境(如开发、测试...

    JSONConfig:抽象重新配置文件类,交替xml,同时支持json文件

    JSONConfig 是一个专门为 C# 开发的抽象配置文件类,旨在提供一种替代传统 XML 配置文件的方法,同时也支持 JSON 文件格式。这个库允许开发者在 XML 和 JSON 之间灵活切换,以便于管理和处理应用程序的配置数据。...

    JsonConfig:基于json的配置框架

    配置文件JsonConfig 是一个简单的配置框架,基于 json 和 .NET Framework 4.0+ 中可用的动态类型入门在您的项目中,添加对 JsonConfig.dll 的引用使用 json 格式的配置将名为“app.json.config”的文件添加到您的...

    JsonConfig:使用JSON和C#4.0动态功能的简单配置库

    JsonConfig自述文件关于JsonConfig是一个易于使用的配置库,它允许C#/。NET应用程序使用基于JSON的配置文件,而不必使用繁琐的web.config / application.config xml文件。 它基于JsonFX和C#4.0动态功能。 允许将...

    ezmorph-1.0.6.jar

    缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig 使用json时候将会用到的一个jar包,发现这个包在网上提供的比较少.缺少这个包可能导致Could not initialize class net.sf.json.JsonConfig ...

    json-lib-2.1 2.2 2.3 2.4-jdk15

    JSONArray.fromObject(map)报错:Could not initialize class net.sf.json.JsonConfig。ireport 需要高于2.1版本的包。于是就找了这些包。最后2.2.2适合

    java jsonto对象互转

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setIgnoreDefaultExcludes(false); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); jsonConfig.registerJsonValueProcessor...

    json转对象数组与对象数组转json --Java

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); jsonConfig.setRootClass(PropertyT.class); PropertyT[] mProperties = (PropertyT[]) JSONSerializer....

    改良版的json-lib2.4

    JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter(){ public boolean apply(Object source, String name, Object value) { if(name.equals("parentGroup") || name.equals(...

    java.util.Date到Json日期

    JsonConfig jsonConfig = new JsonConfig(); ``` 2. **注册JsonValueProcessor**:接下来,我们需要注册一个`JsonValueProcessor`来处理`java.util.Date`类型的值。 ```java jsonConfig....

    java json-lib解决无循环的探索例子

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setMaxDepth(5); // 设置最大深度为5 ``` 在你的工程文件`JsonLibTest`中,可能包含了对这个问题的实验性解决方法。你可以通过运行这个测试类,查看其如何...

    json文件读写实例

    JsonConfig jsonConfig = new JsonConfig(); // 这里可以根据需求配置序列化选项 try { // 将JSON对象写入文件 obj.writeToFile("path_to_output_file.json", "UTF-8", jsonConfig); } catch (IOException e)...

    使用json-lib自定义复杂类型转换为Json字符串

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerConverter(new AddressConverter()); Person person = new Person(); person.setName("John"); person.setAge(30); person.setAddress(new Address...

    java-json-case-src.zip

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(new YourCustomProcessor()); String jsonString = "..."; YourCustomClass customClass = (YourCustomClass) ...

    jsonlib需要jar包

    JSONConfig jsonConfig = new JSONConfig(); jsonConfig.setIgnoreDefaultExcludes(true); jsonConfig.setUseSmart(true); // 转换为JSON JSONObject jsonObject = JSONObject.fromObject(myObject, jsonConfig); `...

    java处理json对象

    JsonConfig jsonConfig = new JsonConfig(); JSONObject jsonData = JSONObject.fromObject(data, jsonConfig); System.out.println(jsonData.toString()); ``` 这段代码会输出类似`{"name":"John","age":30,"city":...

    使用json-lib进行Java和JSON之间的转换

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(User.class); // 指定目标Java类 jsonConfig.setIgnoreDefaultExcludes(false); // 是否忽略默认的排除字段 // 添加自定义转换规则... ...

    json-lib-2.4-jdk15.jar及其相关依赖

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setIgnoreDefaultExcludes(true); String jsonString = JSONObject.toJSONString(person, jsonConfig); ``` 3. **反序列化**: 同样,`json-lib`也提供了反...

    json-lib 使用总结--java对象转json字符串

    JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig); ``` 标签中的“源码”提示我们关注 `json-...

    json的讲解

    JSONConfig jsonConfig = new JSONConfig().setValueProcessor(new MyDateProcessor()); JSONArray.fromObj(srcObj, jsonConfig); ``` 总之,JSON作为一种轻量级的数据交换格式,不仅简单易用,而且在各种编程语言...

Global site tag (gtag.js) - Google Analytics