• Json lib


    Json_lib可以方便的将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将Json字符串转换成Java对象,或者将xml字符串转换成Java对象。

    官网:http://json-lib.sourceforge.net/

    JSON在线转换:http://json.parser.online.fr/

    JSON教程:http://www.json.org/json-zh.html

    官网上说明了json_lib还需要依赖的Jar包有:

    JAR

    网址

    jakarta commons-lang 2.5

    http://commons.apache.org/lang/download_lang.cgi

    jakarta commons-beanutils 1.8.0

    http://commons.apache.org/beanutils/download_beanutils.cgi

    jakarta commons-collections 3.2.1

    http://commons.apache.org/collections/download_collections.cgi

    jakarta commons-logging 1.1.1

    http://commons.apache.org/logging/download_logging.cgi

    ezmorph 1.0.6

    http://ezmorph.sourceforge.net/

    注意这里如果jakarta commons-lang 使用3.1版本的会报:Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

    所以这里还是使用2.6的吧

    MyBean.java:

     1     package com.iflytek.json;  
     2       
     3     import net.sf.json.JSONFunction;  
     4       
     5     /** 
     6      * @author xudongwang 2012-1-15 
     7      *  
     8      *         Email:xdwangiflytek@gmail.com 
     9      */  
    10     public class MyBean {  
    11         private String name = "json";  
    12         private int pojoId = 1;  
    13         private char[] options = new char[] { 'a', 'f' };  
    14         private String func1 = "function(i){ return this.options[i]; }";  
    15         private JSONFunction func2 = new JSONFunction(new String[] { "i" },  
    16                 "return this.options[i];");  
    17           
    18           
    19         public String getName() {  
    20             return name;  
    21         }  
    22       
    23         public void setName(String name) {  
    24             this.name = name;  
    25         }  
    26       
    27         public int getPojoId() {  
    28             return pojoId;  
    29         }  
    30       
    31         public void setPojoId(int pojoId) {  
    32             this.pojoId = pojoId;  
    33         }  
    34       
    35         public char[] getOptions() {  
    36             return options;  
    37         }  
    38       
    39         public void setOptions(char[] options) {  
    40             this.options = options;  
    41         }  
    42       
    43         public String getFunc1() {  
    44             return func1;  
    45         }  
    46       
    47         public void setFunc1(String func1) {  
    48             this.func1 = func1;  
    49         }  
    50       
    51         public JSONFunction getFunc2() {  
    52             return func2;  
    53         }  
    54       
    55         public void setFunc2(JSONFunction func2) {  
    56             this.func2 = func2;  
    57         }  
    58       
    59     }  
    JsonlibTest.java:
      1     package com.iflytek.json;  
      2       
      3     import java.lang.reflect.InvocationTargetException;  
      4     import java.util.ArrayList;  
      5     import java.util.HashMap;  
      6     import java.util.List;  
      7     import java.util.Map;  
      8       
      9     import net.sf.json.JSONArray;  
     10     import net.sf.json.JSONObject;  
     11       
     12     import org.apache.commons.beanutils.PropertyUtils;  
     13       
     14     /** 
     15      * @author xudongwang 2012-1-15 
     16      *  
     17      *         Email:xdwangiflytek@gmail.com 
     18      */  
     19     public class JsonlibTest {  
     20       
     21         // JSONArray是将一个Java对象转换成json的Array格式,如['xdwang', 22]  
     22         private JSONArray jsonArray = null;  
     23         // JSONObject是将Java对象转换成一个json的Object形式,如{name:'xdwang', age: 22}  
     24         private JSONObject jsonObject = null;  
     25       
     26         public static void main(String[] args) {  
     27             JsonlibTest json = new JsonlibTest();  
     28             json.ArrayToJSON();  
     29             json.ListToJSON();  
     30             json.MapsToJSON();  
     31             json.BeanToJSON();  
     32             json.JSONToBean();  
     33         }  
     34       
     35         /** 
     36          * 数组转JSON操作 
     37          */  
     38         public void ArrayToJSON() {  
     39             boolean[] boolArray = new boolean[] { true, false, true };  
     40             jsonArray = JSONArray.fromObject(boolArray);  
     41             System.out.println("数组转JSON操作:" + jsonArray);  
     42         }  
     43       
     44         /** 
     45          * 集合转JSON操作 
     46          */  
     47         public void ListToJSON() {  
     48             List<String> list = new ArrayList<String>();  
     49             list.add("first");  
     50             list.add("second");  
     51             jsonArray = JSONArray.fromObject(list);  
     52             System.out.println("集合转JSON操作:" + jsonArray);  
     53         }  
     54       
     55         /** 
     56          * Maps转JSON操作 
     57          */  
     58         public void MapsToJSON() {  
     59             Map<String, Object> map = new HashMap<String, Object>();  
     60             map.put("name", "json");  
     61             map.put("bool", Boolean.TRUE);  
     62             map.put("int", new Integer(1));  
     63             map.put("arr", new String[] { "a", "b" });  
     64             map.put("func", "function(i){ return this.arr[i]; }");  
     65             jsonObject = JSONObject.fromObject(map);  
     66             System.out.println("Maps转JSON操作:" + jsonObject);  
     67         }  
     68       
     69         /** 
     70          * Bean转JSON操作 
     71          */  
     72         public void BeanToJSON() {  
     73             jsonObject = JSONObject.fromObject(new MyBean());  
     74             System.out.println("Bean转JSON操作:" + jsonObject);  
     75         }  
     76       
     77         /** 
     78          * JSON转Bean操作 
     79          */  
     80         public void JSONToBean() {  
     81             try {  
     82                 String json = "{"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"name":"json","options":["a","f"],"pojoId":1}";  
     83                 jsonObject = JSONObject.fromObject(json);  
     84                 Object bean = JSONObject.toBean(jsonObject);  
     85                 System.out.println("jsonStr:" + json);  
     86       
     87                 System.out.println("name:" + jsonObject.get("name"));  
     88                 System.out.println("name:"  
     89                         + PropertyUtils.getProperty(bean, "name"));  
     90                 System.out.println("pojoId:" + jsonObject.get("pojoId"));  
     91                 System.out.println("pojoId:"  
     92                         + PropertyUtils.getProperty(bean, "pojoId"));  
     93                 System.out.println("options:" + jsonObject.get("options"));  
     94                 System.out.println("options:"  
     95                         + PropertyUtils.getProperty(bean, "options"));  
     96                 System.out.println("func1:" + jsonObject.get("func1"));  
     97                 System.out.println("func1:"  
     98                         + PropertyUtils.getProperty(bean, "func1"));  
     99                 System.out.println("func2:" + jsonObject.get("func2"));  
    100                 System.out.println("func2:"  
    101                         + PropertyUtils.getProperty(bean, "func2"));  
    102             } catch (IllegalAccessException e) {  
    103                 e.printStackTrace();  
    104             } catch (InvocationTargetException e) {  
    105                 e.printStackTrace();  
    106             } catch (NoSuchMethodException e) {  
    107                 e.printStackTrace();  
    108             }  
    109         }  
    110       
    111     }  
      

    这里就不描述xml的相关操作了,具体可以查看文档。

  • 相关阅读:
    Css_加载样式
    Mvc4_@RenderBody()和@RenderSection()
    C#_观察者模式
    Mvc4_MvcPager 概述
    Mvc4_Area的应用
    Nginx 服务器性能参数设置
    Nginx变量的实现机制
    天下无雾
    Nginx Http框架的理解
    【转】websocket协议规范
  • 原文地址:https://www.cnblogs.com/ixiaoge/p/3654572.html
Copyright © 2020-2023  润新知