日期:2014-05-16  浏览次数:20370 次

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);  
              
            //添加属性