@@ -60,21 +60,24 @@ public static String getCorrectJson(String s) {
6060 public static String getCorrectJson (String s , boolean isArray ) {
6161 s = StringUtil .getTrimedString (s );
6262 // if (isArray) {
63- // if (s.startsWith("\"")) {
63+ // while (s.startsWith("\"")) {
6464 // s = s.substring(1);
6565 // }
66- // if (s.endsWith("\"")) {
66+ // while (s.endsWith("\"")) {
6767 // s = s.substring(0, s.length() - 1);
6868 // }
6969 // }
7070 return s ;//isJsonCorrect(s) ? s : null;
7171 }
7272
73- /**json转JSONObject
73+ /**obj转JSONObject
7474 * @param json
7575 * @return
7676 */
7777 public static JSONObject parseObject (Object obj ) {
78+ if (obj instanceof JSONObject ) {
79+ return (JSONObject ) obj ;
80+ }
7881 return parseObject (toJSONString (obj ));
7982 }
8083 /**json转JSONObject
@@ -114,12 +117,16 @@ public static <T> T parseObject(JSONObject object, Class<T> clazz) {
114117 * @return
115118 */
116119 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+ }
123130 }
124131 return null ;
125132 }
@@ -131,6 +138,16 @@ public static <T> T parseObject(String json, Class<T> clazz) {
131138 public static JSONArray parseArray (List <Object > list ) {
132139 return new JSONArray (list );
133140 }
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+ }
134151 /**json转JSONArray
135152 * @param json
136153 * @return
@@ -157,10 +174,14 @@ public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) {
157174 * @return
158175 */
159176 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+ }
164185 }
165186 return null ;
166187 }
0 commit comments