Java里如何应用Json格式数据

2012-11-12
    JSON是一种很简洁很重要的数据格式,通常用来交换传输数据,广泛使用于JavaScript技术中,并逐渐在各种流行编程语言中火了起来。在Java中也有一个JSON的库,用来重要作用就是Java对象与JSON、XML数据的相互转换,有着重要的应用。
    环境:JDK5 , json-lib-2.3-jdk15
    所依赖的包:json-lib-2.3-jdk15.jar,commons-collections.jar,commons- lang.jar,commons-logging.jar,commons-beanutils.jar,ezmorph-1.0.6.jar,xom-1.1.jar
    java中各种类型所对应的json格式:
    1.数组或集合-->JSON串
    public static void test1() {
    System.out.println("------------数组或集合-->JSON串----------");
    boolean[] boolArray = new boolean[]{true, false, true};
    JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
    System.out.println(jsonArray1);
    //输出格式: [true,false,true]
    List list = new ArrayList();
    list.add("first");
    list.add("second");
    JSONArray jsonArray2 = JSONArray.fromObject(list);
    System.out.println(jsonArray2);
    //输出格式: ["first","second"]
    JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
    System.out.println(jsonArray3);
    //输出格式: ["json","is","easy"]
    }
    2.Object|Map-->JSON串
    public static void test2() {
    System.out.println("------------Object|Map-->JSON串----------");
    Map map = new HashMap();
    map.put("name", "json");
    map.put("bool", Boolean.TRUE);
    map.put("int", new Integer(1));
    map.put("arr", new String[]{"a", "b"});
    map.put("func", "function(i){ return this.arr[i]; }");
    JSONObject jsonObject1 = JSONObject.fromObject(map);
    System.out.println(jsonObject1);
    //输出格式: {"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"bool":true,"name":"json"}
    JSONObject jsonObject2 = JSONObject.fromObject(new MyBean());
    System.out.println(jsonObject2);
    //输出格式: {"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"name":"json","options":["a","f"],"pojoId":1}
    }
    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];");
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getPojoId() {
    return pojoId;
    }
    public void setPojoId(int pojoId) {
    this.pojoId = pojoId;
    }
    public char[] getOptions() {
    return options;
    }
    public void setOptions(char[] options) {
    this.options = options;
    }
    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;
    }
    }

    考试大温馨提示:本内容来源于网络,仅代表作者个人观点,与本站立场无关,仅供您学习交流使用。其中可能有部分文章经过多次转载而造成文章内容缺失、错误或文章作者不详等问题,请您谅解。如有侵犯您的权利,请联系我们,本站会立即予以处理。

    编辑特别推荐:

    Java认证如何查看ControlFile中的内容  

    Java中基本数据类型与应用

    Servlet中的八大Listener

分享到:
0
相关阅读
友情链接
© 2018 我考网 http://www.woexam.com 中国互联网举报中心 湘ICP备18023104号 京公网安备 11010802020116号
违法和不良信息举报:9447029@qq.com