我一直以为JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承。经过研究JSONObject和JSONArray的结构,咱们顺便也复习一下JDK的内容。
首先看一下JSONObject和JSONArray 的结构:ide
1 final class JSONObject extends AbstractJSON implements JSON, Map, Comparable 2 final class JSONArray extends AbstractJSON implements JSON, List, Comparable
首先看看JSON接口:JSON extends Serializable,这一点代表JSONObject和JSONArray是能够实现序列化的。JSON接口的具体的定义也是针对很经常使用的功能:函数
1 boolean isArray(); 2 boolean isEmpty(); 3 int size();//对于JSONObject来讲是Bean属性的个数,对于JSONArray来讲是Bean的个数 4 String toString( int indentFactor ); 5 String toString( int indentFactor, int indent ); 6 Writer write( Writer writer );
接着看一下Map接口:spa
1 int size(); 2 boolean isEmpty(); 3 boolean containsKey(Object key); 4 boolean containsValue(Object value); 5 V get(Object key); 6 V put(K key, V value); 7 V remove(Object key); 8 void putAll(Map<? extends K, ? extends V> m); 9 void clear(); 10 Set<K> keySet(); 11 Collection<V> values(); 12 Set<Map.Entry<K, V>> entrySet();
上面的一个函数引出了另一个接口:Entry<K,V>
再接着看一下List接口:code
1 interface List<E> extends Collection<E> 2 Interface Collection<E> extends Iterable<E>
在List接口中要注意:blog
1 ListIterator<E> listIterator(); 2 ListIterator<E> listIterator(int index);