JsonMap.java
5.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.teplot.common;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.log.Log;
/**
* Depiction:Json处理工具类,避免了编写大量javabean的问题
* <p/>
* Modify:
* <p/>
* Author: Kevin Lynn
* <p/>
* Create Date:2014-4-8 下午5:28:12
* <p/>
*
* @version 1.0
* @since 1.0
*/
public class JsonMap extends HashMap<String, Object> {
private static final long serialVersionUID = 4567321902312180302L;
public boolean getBoolean(String key) {
try {
return getString(key).equals("true");
} catch (Exception e) {
}
return false;
}
public int getInt(String key) {
try {
return (int) getFloat(key);
} catch (Exception e) {
}
return 0;
}
public float getFloat(String key) {
try {
return Float.parseFloat(get(key).toString());
} catch (Exception e) {
}
return 0.0f;
}
public double getDouble(String key) {
try {
return Double.parseDouble(get(key).toString());
} catch (Exception e) {
}
return 0.0f;
}
public long getLong(String key) {
try {
return (long) getDouble(key);
} catch (Exception e) {
}
return 0l;
}
public String getString(String key) {
return get(key) != null ? get(key).toString() : null;
}
@SuppressWarnings ("unchecked")
public JsonMap getMap(String key) {
try {
Map<String, Object> map = (Map<String, Object>) get(key);
JsonMap data = new JsonMap();
for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
String k = (String) keys.next();
Object v = map.get(k);
data.put(k, v);
}
return data;
} catch (Exception e) {
Log.getLog(this.getClass()).error("getMap()" + e.toString());
}
return null;
}
@SuppressWarnings ("unchecked")
public List<JsonMap> getListMap(String key) {
try {
List<Map<String, Object>> maps = (List<Map<String, Object>>) get(key);
List<JsonMap> listMap = new ArrayList<JsonMap>();
for (Map<String, Object> map : maps) {
JsonMap data = new JsonMap();
for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
String k = (String) keys.next();
Object v = map.get(k);
data.put(k, v);
}
listMap.add(data);
}
return listMap;
} catch (Exception e) {
Log.getLog(this.getClass()).error( "getListMap()" + e.toString());
}
return null;
}
@SuppressWarnings ("unchecked")
public List<String> getStringList(String key) {
try {
List<String> strings = (List<String>) get(key);
return strings;
} catch (Exception e) {
Log.getLog(this.getClass()).error( "getStringList()" + e.toString());
}
return null;
}
@SuppressWarnings ("unchecked")
public List<Double> getDoubleList(String key) {
try {
List<Double> doubles = (List<Double>) get(key);
return doubles;
} catch (Exception e) {
Log.getLog(this.getClass()).error( "getDoubleList()" + e.toString());
}
return null;
}
public static JsonMap toJsonMap(Map<String, Object> map) {
JsonMap data = new JsonMap();
for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
String k = (String) keys.next();
Object v = map.get(k);
data.put(k, v);
}
return data;
}
/**
* Json数据解析
*
* @param json
* json源串
* @return JsonMap
*/
public static JsonMap parseJson(String json) {
return parseJson(json, JsonMap.class);
}
/**
* Json数据解析
*
* @param json
* json源串
* @return List<JsonMap>
*/
public static List<JsonMap> parseJsonArray(String json) {
List<JsonMap> listMap = new ArrayList<JsonMap>();
try {
Type listType = new TypeToken<List<Map<String, Object>>>() {
}.getType();
List<Map<String, Object>> list = parseJson(json, listType);
for (Map<String, Object> map : list) {
listMap.add(toJsonMap(map));
}
} catch (Exception e) {
Log.getLog(new JsonMap().getClass()).error( "parseJsonArray()" + e.toString());
}
return listMap;
}
/**
* Json数据解析
*
* @param <T>
* @param json
* json源串
* @return List<T>
*/
public static <T> List<T> parseArray(String json) {
List<T> listMap = new ArrayList<T>();
try {
Type listType = new TypeToken<List<T>>() {
}.getType();
listMap = parseJson(json, listType);
} catch (Exception e) {
Log.getLog(new JsonMap().getClass()).error( "parseArray()" + e.toString());
}
return listMap;
}
/**
* Json数据解析
*
* @param json
* json源串
* @param cls
* 存储json的实体类
* @return 相应的实体类对象
*/
public static <T> T parseJson(String json, Type cls) {
try {
Gson gson = new Gson();
return gson.fromJson(json, cls);
} catch (Exception e) {
Log.getLog(new JsonMap().getClass()).error("parseJson()" + e.getMessage());
}
return null;
}
@Override
public Object get(Object key) {
return super.get(key);
}
@Override
public String toString() {
try {
Gson gson = new Gson();
return gson.toJson(this);
} catch (Exception e) {
Log.getLog(new JsonMap().getClass()).error("toString()" + e.getMessage());
}
return super.toString();
}
}