代码片段(1)[全屏查看所有代码]
1. [代码]JSONHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
//JSONHelper.java
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Set;
import
net.sf.json.JSONArray;
import
net.sf.json.JSONObject;
public
class
JSONHelper {
/**
* 将JSONObjec对象转换成Map-List集合
* @see JSONHelper#reflect(JSONArray)
* @param json
* @return
*/
public
static
HashMap<String, Object> reflect(JSONObject json){
HashMap<String, Object> map =
new
HashMap<String, Object>();
Set<?> keys = json.keySet();
for
(Object key : keys){
Object o = json.get(key);
if
(o
instanceof
JSONArray)
map.put((String) key, reflect((JSONArray) o));
else
if
(o
instanceof
JSONObject)
map.put((String) key, reflect((JSONObject) o));
else
map.put((String) key, o);
}
return
map;
}
/**
* 将JSONArray对象转换成Map-List集合
* @see JSONHelper#reflect(JSONObject)
* @param json
* @return
*/
public
static
Object reflect(JSONArray json){
List<Object> list =
new
ArrayList<Object>();
for
(Object o : json){
if
(o
instanceof
JSONArray)
list.add(reflect((JSONArray) o));
else
if
(o
instanceof
JSONObject)
list.add(reflect((JSONObject) o));
else
list.add(o);
}
return
list;
}
}
|