json教程系列(3)-JSONObject的过滤设置
2015-09-06 10:37
656 查看
我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值。例如下面的类:
public class Person
{
private String name;
private String address;
private String sex;
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
}如果我想过滤address属性怎么办?
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class Test
{
public static void main(String args[])
{
Person person = new Person();
person.setName("swiftlet");
person.setSex("men");
person.setAddress("china");
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]
{ "address" });
JSONObject json = JSONObject.fromObject(person, jsonConfig);
System.out.println(json.toString());
}
}
public class Person
{
private String name;
private String address;
private String sex;
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
}如果我想过滤address属性怎么办?
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class Test
{
public static void main(String args[])
{
Person person = new Person();
person.setName("swiftlet");
person.setSex("men");
person.setAddress("china");
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[]
{ "address" });
JSONObject json = JSONObject.fromObject(person, jsonConfig);
System.out.println(json.toString());
}
}
相关文章推荐
- json教程系列(2)-生成JSONObject的方法
- iOS7之后JavaScript与Objective-C之间的通信
- dop object 中的属性运用
- OC基础教程1-Hello Wrold(OS X版)
- JSONObject与JSONArray的使用
- Objective-C 面向对象(类II)
- error: allocating an object of abstract class type
- Effective Objective-C 2.0 第5条:用枚举表示状态、选项、状态码
- Objective-C 内存管理
- lodash 中文学习拾零之 Object篇
- swift详解之十-------------异常处理、类型转换 ( Any and AnyObject )
- 黑马程序员——Objective-C之面向对象
- Objective-c 循环引用
- Flex中用RemoteObject与java后台通信
- Hibernate 异常:object references an unsaved transient instance - save the transient instance before fl
- object references an unsaved transient instance - save the transient instance before flushing
- 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)
- IOS学习笔记3—Objective C—简单的内存管理
- IOS学习笔记2—Objective C—类、属性、方法
- Object-C中获取当前触摸点的坐标位置