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
153 lines (136 loc) · 3.88 KB
/
Copy pathJSON.java
File metadata and controls
153 lines (136 loc) · 3.88 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
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package zuo.biao.apijson;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**阿里json封装类 防止解析时异常
* @author Lemon
*/
public class JSON {
private static final String TAG = "JSON";
/**判断json格式是否正确
* @param s
* @return
*/
public static boolean isJsonCorrect(String s) {
System.out.println(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) {
s = StringUtil.getNoBlankString(s);
return isJsonCorrect(s) ? s : "";
}
/**json转JSONObject
* @param json
* @return
*/
public static JSONObject parseObject(String json) {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
return parseObject(json, features);
}
/**json转JSONObject
* @param json
* @param features
* @return
*/
public static JSONObject parseObject(String json, int features) {
try {
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
} catch (Exception e) {
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
}
return null;
}
/**json转实体类
* @param json
* @param clazz
* @return
*/
public static <T> T parseObject(String json, Class<T> clazz) {
try {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
} catch (Exception e) {
System.out.println(TAG + "parseObject catch \n" + e.getMessage());
}
return null;
}
/**json转JSONArray
* @param json
* @return
*/
public static JSONArray parseArray(String json) {
return com.alibaba.fastjson.JSON.parseArray(json);
}
/**json转实体类列表
* @param json
* @param clazz
* @return
*/
public static <T> List<T> parseArray(String json, Class<T> clazz) {
try {
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json), clazz);
} catch (Exception e) {
System.out.println(TAG + "parseArray catch \n" + e.getMessage());
}
return null;
}
/**实体类转json
* @param obj
* @return
*/
public static String toJSONString(Object obj) {
try {
return com.alibaba.fastjson.JSON.toJSONString(obj);
} catch (Exception e) {
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
}
return null;
}
/**实体类转json
* @param obj
* @param features
* @return
*/
public static String toJSONString(Object obj, SerializerFeature... features) {
try {
return com.alibaba.fastjson.JSON.toJSONString(obj, features);
} catch (Exception e) {
System.out.println(TAG + "toJSONString catch \n" + e.getMessage());
}
return null;
}
/**格式化,显示更好看
* @param json
* @return
*/
public static String format(String json) {
return toJSONString(parseObject(json), SerializerFeature.PrettyFormat);
}
}