Android JSON使用(二):解讀org.json包

   在Android開發文檔中,與JSON相關的類被放置在org.json包中,在這個包中有四個類一個Exception:

   JSONTokener,JSONObject,JSONArray,JSONStringer和JSONException。

   1.JSONTokener類

    這個類的功能就是將按照JSON(RFC 4627)格式的字符竄解析成與JSON規範一致的對象。大多數用戶只是使用到這個類的構造方法和nextValue()方法。

    構造方法:

        JSONTokener(String in) ,參數是符合JSON(RFC 4627)規範的字符串。

    public method:

        Object nextValue( ) , Return the next value from the input。

    Example:

     String json = "{"

          + "  \"query\": \"Pizza\", "

          + "  \"locations\": [ 94043, 90210 ] "

          + "}";

     JSONObject object = (JSONObject) new JSONTokener(json).nextValue();

     String query = object.getString("query");

     JSONArray locations = object.getJSONArray("locations");

  2.JSONObject類

    這是一個可變的鍵值對映射的集合,這個集合中鍵值對可增加、減少,值可以是JSONObjects,JSONArrays,Strings,Booleans,Integers,Longs,Double or NULL,總之值必須是對象的應用。

   常用方法介紹:

   構造方法:

       JSONObject( ), creates a JSONObject with no name/value mappings

       JSONObject(JSONTokener readForm), creates a new JSONObject with name/value mappings from the next object in the tokener

       JSONObject(String json), creates a new JSONObject with name/value mappings from the JSON String

  public methods:

       JSONObject accumulate(String name, Object value), Appends value to the array already mapped to name.

       Object get(String name), Returns the value mapped by name.

       JSONArray getJSONArray(String name), Returns the value mapped by name if it exists and is a JSONArray.

       JSONObject getJSONObject(String name), Return the value mapped by name if it exist and is a JSONOBject.

       String toString( ), Enables this object as a compact JSON String.

       String toString(int indentSpaces), Enables this object as a human readable JSON String for debugging.

       剩下幾個比較重要的方法:getBoolean(), getDouble(), getInt(), getLong(), getString()。

   3.JSONArray類

     這是一個值的索引序列,值可以是JSONObjects,其他的JSONArray, Strings, Booleans, Integers, Longs, Doubles,null or NULL.

     構造方法:

         JSONArray(),creates a JSONArray with no values.

         JSONArray(JSONTokener readFrom), create a new JSONArray with values from the next array in the tokener. 

         JSONArray(String json), creates a new JSONArray with values from the JSON String.

     public methods:

         JSONArray getJSONArray(int index), Returns the value at index if it exists and is a JSONArray.

         JSONObject getJSONObject(int index), Returns the value at index if it exists and is a JSONObject.

         Object get(int index), Returns the value at index.

        剩下幾個比較重要的方法,是對方法get(int index)的細化:getDouble(int index), getBoolean(int index), getInt(int index), getLong(int index), getString(int index).

    4.JSONStringer類

      這個類實現了toString() and toString()方法。大多數應用程序開發者直接使用自帶的toString()方法,而不是用這個類的方法。

        5. JSONExceptionl類

      這個類不做介紹。

 總結:

    1.雖然 JSONTokener,JSONObject,JSONArray,JSONStringer這幾個類都不是final類,但在使用這幾個類的時候,最好不要試圖去繼承它們。

    2.JSONTokener,JSONObject,JSONArray,JSONStringer這幾個類都不是線程安全的類。

    3.JSONObject和JSONArray,使用了兩種相互衝突的方式表現null:一種是標準的Java null引用,另一種是包裝值NULL。特別是,JSONObject在調用put(name, null)會清除掉name指定的實體對象,但調用put(name, JSONObject.NULL)會存儲這個鍵值對,其value爲JSONObject.NULL

           

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章