JSON - LIB使用

来源:互联网 发布:阿里云 宕机 编辑:程序博客网 时间:2024/05/18 02:30

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);
    }
}

原创粉丝点击