JSONObject和JSONArray是JDK的集合部分延伸

我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承。通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容。
首先看一下JSONObject和JSONArray 的结构:

1 final class JSONObject extends AbstractJSON implements JSON, Map, Comparable
2 final class JSONArray extends AbstractJSON implements JSON, List, Comparable
View Code

首先看看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 );
View Code

接着看一下Map接口:

 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();
View Code

上面的一个函数引出了另外一个接口:Entry<K,V>
再接着看一下List接口:

1 interface List<E> extends Collection<E>
2 Interface Collection<E> extends Iterable<E>
View Code

在List接口中要注意:

1 ListIterator<E> listIterator();
2 ListIterator<E> listIterator(int index);
View Code

 

posted @ 2017-03-23 22:06  奔跑8蜗牛  阅读(239)  评论(0编辑  收藏  举报