JSON - LIB使用

json-lib使用

发布者:周围,发布时间:2010年1月20日 上午1:56[更新时间:2010年5月25日 下午8:54]
  • 数组 -> json
public void arr2json() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
// prints [true,false,true]
}
public void list2json() {
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
// prints ["first","second"]
}

public void createJson() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray);
// prints ["json","is","easy"]
}

  • 对象 -> json
public void bean2json() {
JSONObject jsonObject = JSONObject.fromObject(new MyBean());
System.out.println(jsonObject);
/*
* prints
* {"func1":function(i){ return this.options[i];
* },"pojoId":1,"name":"json","func2":function(i){ return
* this.options[i]; }}
*/
}

  • json -> 对象
public void json2bean() {
String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2'],\"options2\":\"\"}";
JSONObject jb = JSONObject.fromObject(json);
MyBean temp = (MyBean) JSONObject.toBean(jb, MyBean.class);

System.out.println(temp.getName());
}

public class MyBean {
private String name = "json";
private int pojoId = 1;
private char[] options = new char[] { 'a', 'f' };
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[] { "i" },"return this.options[i];");
private char[] optionss = new char[] { 'a', 'f' };
public String getName() {
return name;
}
public char[] getOptions() {
return options;
}
public void setOptions(char[] options) {
this.options = options;
}
public char[] getOptionss() {
return optionss;
}
public void setOptionss(char[] optionss) {
this.optionss = optionss;
}
public void setName(String name) {
this.name = name;
}
public int getPojoId() {
return pojoId;
}
public void setPojoId(int pojoId) {
this.pojoId = pojoId;
}
public String getFunc1() {
return func1;
}
public void setFunc1(String func1) {
this.func1 = func1;
}
public JSONFunction getFunc2() {
return func2;
}
public void setFunc2(JSONFunction func2) {
this.func2 = func2;
}
}

  • 进阶,
    • 属性过滤
//json转化属性过滤,忽略掉值为null的属性
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if (value == null) {
return true;
}
return false;
}
});

return JSONObject.fromObject(new MyBean(), jsonConfig).toString();
}

  • 日期格式处理
//json 日期处理
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
return JSONObject.fromObject( 对象 , jsonConfig).toString();


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class DateJsonValueProcessor implements JsonValueProcessor {
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
private DateFormat dateFormat;

/**
* 构造方法.
*
* @param datePattern 日期格式
*/
public DateJsonValueProcessor(String datePattern) {
try {
dateFormat = new SimpleDateFormat(datePattern);
} catch (Exception ex) {
dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
}
}

public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}

public Object processObjectValue(String key, Object value,
JsonConfig jsonConfig) {
return process(value);
}

private Object process(Object value) {
return dateFormat.format((Date) value);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值