chenming47 发表于 2013-2-5 09:52:27

json-lib包使用总结

                                        使用JSON的方法
 
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。本文将快速讲解 JSON 格式,并通过代码示例演示如何分别在客户端和服务器端进行 JSON 格式数据的处理。
 
Json必需的包
commons-httpclient-4.1.1jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar
commons-beanutils-1.8.3.jar
以上包可以从
http://commons.apache.org/index.html
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
http://www.docjar.com/
中下载到。
 
测试类一:
 
    import net.sf.json.JSONArray;      import net.sf.json.JSONObject;            public class JSONObjectSample {                  //创建JSONObject对象          private static JSONObject createJSONObject(){            JSONObject jsonObject = new JSONObject();            jsonObject.put("name", "kevin");            jsonObject.put("Max.score", new Integer(100));            jsonObject.put("Min.score", new Integer(50));            jsonObject.put("nickname", "picglet");            return jsonObject;          }          public static void main(String[] args) {            JSONObject jsonObject = JSONObjectSample.createJSONObject();            //输出jsonobject对象            System.out.println("jsonObject==>"+jsonObject);                            //判读输出对象的类型            boolean isArray = jsonObject.isArray();            boolean isEmpty = jsonObject.isEmpty();            boolean isNullObject = jsonObject.isNullObject();            System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject);                            //添加属性            jsonObject.element("address", "swap lake");            System.out.println("添加属性后的对象==>"+jsonObject);                            //返回一个JSONArray对象            JSONArray jsonArray = new JSONArray();            jsonArray.add(0, "this is a jsonArray value");            jsonArray.add(1,"another jsonArray value");            jsonObject.element("jsonArray", jsonArray);            JSONArray array = jsonObject.getJSONArray("jsonArray");            System.out.println("返回一个JSONArray对象:"+array);            //添加JSONArray后的值            //{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",            //"jsonArray":["this is a jsonArray value","another jsonArray value"]}            System.out.println(jsonObject);                            //根据key返回一个字符串            String jsonString = jsonObject.getString("name");            System.out.println("jsonString==>"+jsonString);          }      } 测试类二:
 
   import java.util.ArrayList;      import java.util.HashMap;      import java.util.List;      import java.util.Map;            import net.sf.json.JSONArray;      import net.sf.json.JSONObject;            public class Json {          public static void main(String[] args) {            Json j = new Json();            j.bean2json();          }                public void arr2json() {            boolean[] boolArray = new boolean[] { true, false, true };            JSONArray jsonArray = JSONArray.fromObject(boolArray);            System.out.println(jsonArray);            // prints           }                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"]          }                public void map2json() {            Map<String,String> map = HashMap<String,String>()            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; }");                  JSONObject json = JSONObject.fromObject(map);            System.out.println(json);            // prints            // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){            // return this.arr; }]          }                public void bean2json() {            JSONObject jsonObject = JSONObject.fromObject(new MyBean());            System.out.println(jsonObject);            /*            * prints               * {"func1":function(i){ return this.options;            * },"pojoId":1,"name":"json","func2":function(i){ return            * this.options; }}            */          }                public void json2bean() {            String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";            JSONObject jb = JSONObject.fromString(json);            JSONObject.toBean(jb, MyBean.class);            System.out.println();          }                  public void createAJsonObject(){             JSONObject jsonObject = new JSONObject();             jsonObject.element("name", "周星星");             jsonObject.element("sex", "male");             jsonObject.element("age", 18);             jsonObject.element("job", "student");             System.out.println(jsonObject.get("name"));// 周星星             System.out.println(jsonObject.get("job"));// student             System.out.println(jsonObject.getString("sex"));// male             System.out.println(jsonObject.getInt("age"));// 18                            String json = "{name:\"周星星\",sex:\"male\",age:18,job:\"student\"}";            JSONObject jsonObject = JSONObject.fromObject(json);            //或者使用 JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(json);            System.out.println(jsonObject.get("name"));// 周星星            System.out.println(jsonObject.get("job"));// student            System.out.println(jsonObject.getString("sex"));// male            System.out.println(jsonObject.getInt("age"));// 18             } } 
   操作的bean:
 
 
    import net.sf.json.JSONFunction;            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; }";          private JSONFunction func2 = new JSONFunction(new String[] { "i" },                  "return this.options;");                // getters & setters          ......      }
页: [1]
查看完整版本: json-lib包使用总结