forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.java
More file actions
executable file
·272 lines (254 loc) · 6.4 KB
/
JSON.java
File metadata and controls
executable file
·272 lines (254 loc) · 6.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.List;
/**阿里FastJSON封装类 防止解析时异常
* @author Lemon
*/
public class JSON {
private static final String TAG = "JSON";
/**判断json格式是否正确
* @param s
* @return
*/
public static boolean isJsonCorrect(String s) {
//太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>");
if (s == null
// || s.equals("[]")
// || s.equals("{}")
|| s.equals("")
|| s.equals("[null]")
|| s.equals("{null}")
|| s.equals("null")) {
return false;
}
return true;
}
/**获取有效的json
* @param s
* @return
*/
public static String getCorrectJson(String s) {
return getCorrectJson(s, false);
}
/**获取有效的json
* @param s
* @param isArray
* @return
*/
public static String getCorrectJson(String s, boolean isArray) {
s = StringUtil.getTrimedString(s);
// if (isArray) {
// while (s.startsWith("\"")) {
// s = s.substring(1);
// }
// while (s.endsWith("\"")) {
// s = s.substring(0, s.length() - 1);
// }
// }
return s;//isJsonCorrect(s) ? s : null;
}
/**
* @param json
* @return
*/
private static final Feature[] DEFAULT_FASTJSON_FEATURES = {Feature.OrderedField, Feature.UseBigDecimal};
public static Object parse(Object obj) {
try {
return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), DEFAULT_FASTJSON_FEATURES);
} catch (Exception e) {
Log.i(TAG, "parse catch \n" + e.getMessage());
}
return null;
}
/**obj转JSONObject
* @param obj
* @return
*/
public static JSONObject parseObject(Object obj) {
if (obj instanceof JSONObject) {
return (JSONObject) obj;
}
return parseObject(toJSONString(obj));
}
/**json转JSONObject
* @param json
* @return
*/
public static JSONObject parseObject(String json) {
return parseObject(json, JSONObject.class);
}
/**json转实体类
* @param json
* @param clazz
* @return
*/
public static <T> T parseObject(String json, Class<T> clazz) {
if (clazz == null || StringUtil.isEmpty(json, true)) {
Log.e(TAG, "parseObject clazz == null || StringUtil.isEmpty(json, true) >> return null;");
} else {
try {
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, DEFAULT_FASTJSON_FEATURES);
} catch (Exception e) {
Log.i(TAG, "parseObject catch \n" + e.getMessage());
}
}
return null;
}
/**list转JSONArray
* @param list
* @return
*/
public static JSONArray parseArray(List<Object> list) {
return new JSONArray(list);
}
/**obj转JSONArray
* @param obj
* @return
*/
public static JSONArray parseArray(Object obj) {
if (obj instanceof JSONArray) {
return (JSONArray) obj;
}
return parseArray(toJSONString(obj));
}
/**json转JSONArray
* @param json
* @return
*/
public static JSONArray parseArray(String json) {
if (StringUtil.isEmpty(json, true)) {
Log.e(TAG, "parseArray StringUtil.isEmpty(json, true) >> return null;");
} else {
try {
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true));
} catch (Exception e) {
Log.i(TAG, "parseArray catch \n" + e.getMessage());
}
}
return null;
}
/**JSONArray转实体类列表
* @param array
* @param clazz
* @return
*/
public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
return parseArray(toJSONString(array), clazz);
}
/**json转实体类列表
* @param json
* @param clazz
* @return
*/
public static <T> List<T> parseArray(String json, Class<T> clazz) {
if (clazz == null || StringUtil.isEmpty(json, true)) {
Log.e(TAG, "parseArray clazz == null || StringUtil.isEmpty(json, true) >> return null;");
} else {
try {
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz);
} catch (Exception e) {
Log.i(TAG, "parseArray catch \n" + e.getMessage());
}
}
return null;
}
/**实体类转json
* @param obj
* @return
*/
public static String toJSONString(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof String) {
return (String) obj;
}
try {
return com.alibaba.fastjson.JSON.toJSONString(obj);
} catch (Exception e) {
Log.e(TAG, "toJSONString catch \n" + e.getMessage());
}
return null;
}
/**实体类转json
* @param obj
* @param features
* @return
*/
public static String toJSONString(Object obj, SerializerFeature... features) {
if (obj == null) {
return null;
}
if (obj instanceof String) {
return (String) obj;
}
try {
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
} catch (Exception e) {
Log.e(TAG, "toJSONString catch \n" + e.getMessage());
}
return null;
}
/**格式化,显示更好看
* @param json
* @return
*/
public static String format(String json) {
return format(parse(json));
}
/**格式化,显示更好看
* @param object
* @return
*/
public static String format(Object object) {
return toJSONString(object, SerializerFeature.PrettyFormat);
}
/**判断是否为JSONObject
* @param obj instanceof String ? parseObject
* @return
*/
public static boolean isJSONObject(Object obj) {
if (obj instanceof JSONObject) {
return true;
}
if (obj instanceof String) {
try {
JSONObject json = parseObject((String) obj);
return json != null && json.isEmpty() == false;
} catch (Exception e) {
Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
}
}
return false;
}
/**判断是否为JSONArray
* @param obj instanceof String ? parseArray
* @return
*/
public static boolean isJSONArray(Object obj) {
if (obj instanceof JSONArray) {
return true;
}
if (obj instanceof String) {
try {
JSONArray json = parseArray((String) obj);
return json != null && json.isEmpty() == false;
} catch (Exception e) {
Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
}
}
return false;
}
/**判断是否为 Boolean,Number,String 中的一种
* @param obj
* @return
*/
public static boolean isBooleanOrNumberOrString(Object obj) {
return obj instanceof Boolean || obj instanceof Number || obj instanceof String;
}
}