JSONObject以及json(轉)

1、JAR包簡介
      要使程序 能夠運行 必須引入JSON-lib包,JSON-lib包同時依賴於如下的JAR包:
      1.commons-lang.jar
      2.commons-beanutils.jar
      3.commons-collections.jar
      4.commons-logging.jar 
      5.ezmorph.jar
      6.json-lib-2.2.2-jdk15.jar
      
2、JSONObject對象 使用
     JSON-lib包是一個beans,collections,maps,java arrays 和XML和JSON互相轉換的包。在本例中,咱們將使用JSONObject類建立JSONObject對象,而後咱們打印這些對象的值。爲了使用 JSONObject對象,咱們要引入"net.sf.json"包。爲了給對象添加元素,咱們要使用put()方法。html

 

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

 

獲得JSONObject對象後咱們就能夠使用它的方法了,能夠查看其API,我給出一個在線 的API

    http://json-lib.sourceforge.net/apidocs/jdk15/index.html
   
Html代碼 
JSONArray的方法測試能夠相似進行  

JSONArray的方法測試能夠相似進行java

相關文章
相關標籤/搜索