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

本文出自 “渴望,只做并做好一件事” 博客,请务必保留此出处http://3688429.blog.51cto.com/3678429/682956

原文地址:https://www.cnblogs.com/LiaoHao/p/3337159.html