@@ -60,21 +60,24 @@ public static String getCorrectJson(String s) {
60
60
public static String getCorrectJson (String s , boolean isArray ) {
61
61
s = StringUtil .getTrimedString (s );
62
62
// if (isArray) {
63
- // if (s.startsWith("\"")) {
63
+ // while (s.startsWith("\"")) {
64
64
// s = s.substring(1);
65
65
// }
66
- // if (s.endsWith("\"")) {
66
+ // while (s.endsWith("\"")) {
67
67
// s = s.substring(0, s.length() - 1);
68
68
// }
69
69
// }
70
70
return s ;//isJsonCorrect(s) ? s : null;
71
71
}
72
72
73
- /**json转JSONObject
73
+ /**obj转JSONObject
74
74
* @param json
75
75
* @return
76
76
*/
77
77
public static JSONObject parseObject (Object obj ) {
78
+ if (obj instanceof JSONObject ) {
79
+ return (JSONObject ) obj ;
80
+ }
78
81
return parseObject (toJSONString (obj ));
79
82
}
80
83
/**json转JSONObject
@@ -114,12 +117,16 @@ public static <T> T parseObject(JSONObject object, Class<T> clazz) {
114
117
* @return
115
118
*/
116
119
public static <T > T parseObject (String json , Class <T > clazz ) {
117
- try {
118
- int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
119
- features |= Feature .OrderedField .getMask ();
120
- return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
121
- } catch (Exception e ) {
122
- Log .i (TAG , "parseObject catch \n " + e .getMessage ());
120
+ if (clazz == null ) {
121
+ Log .e (TAG , "parseObject clazz == null >> return null;" );
122
+ } else {
123
+ try {
124
+ int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
125
+ features |= Feature .OrderedField .getMask ();
126
+ return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
127
+ } catch (Exception e ) {
128
+ Log .i (TAG , "parseObject catch \n " + e .getMessage ());
129
+ }
123
130
}
124
131
return null ;
125
132
}
@@ -131,6 +138,16 @@ public static <T> T parseObject(String json, Class<T> clazz) {
131
138
public static JSONArray parseArray (List <Object > list ) {
132
139
return new JSONArray (list );
133
140
}
141
+ /**obj转JSONArray
142
+ * @param json
143
+ * @return
144
+ */
145
+ public static JSONArray parseArray (Object obj ) {
146
+ if (obj instanceof JSONArray ) {
147
+ return (JSONArray ) obj ;
148
+ }
149
+ return parseArray (toJSONString (obj ));
150
+ }
134
151
/**json转JSONArray
135
152
* @param json
136
153
* @return
@@ -157,10 +174,14 @@ public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
157
174
* @return
158
175
*/
159
176
public static <T > List <T > parseArray (String json , Class <T > clazz ) {
160
- try {
161
- return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ), clazz );
162
- } catch (Exception e ) {
163
- Log .i (TAG , "parseArray catch \n " + e .getMessage ());
177
+ if (clazz == null ) {
178
+ Log .e (TAG , "parseArray clazz == null >> return null;" );
179
+ } else {
180
+ try {
181
+ return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ), clazz );
182
+ } catch (Exception e ) {
183
+ Log .i (TAG , "parseArray catch \n " + e .getMessage ());
184
+ }
164
185
}
165
186
return null ;
166
187
}
0 commit comments