2121import com .alibaba .fastjson .parser .Feature ;
2222import com .alibaba .fastjson .serializer .SerializerFeature ;
2323
24+ import zuo .biao .apijson .server .Log ;
25+
2426/**阿里json封装类 防止解析时异常
2527 * @author Lemon
2628 */
@@ -32,7 +34,7 @@ public class JSON {
3234 * @return
3335 */
3436 public static boolean isJsonCorrect (String s ) {
35- System . out . println (TAG + "isJsonCorrect <<<< " + s + " >>>>>>>" );
37+ Log . i (TAG , "isJsonCorrect <<<< " + s + " >>>>>>>" );
3638 if (s == null
3739 // || s.equals("[]")
3840 // || s.equals("{}")
@@ -50,10 +52,33 @@ public static boolean isJsonCorrect(String s) {
5052 * @return
5153 */
5254 public static String getCorrectJson (String s ) {
53- s = StringUtil .getNoBlankString (s );
54- return isJsonCorrect (s ) ? s : "" ;
55+ return getCorrectJson (s , false );
56+ }
57+ /**获取有效的json
58+ * @param s
59+ * @param isArray
60+ * @return
61+ */
62+ public static String getCorrectJson (String s , boolean isArray ) {
63+ s = StringUtil .getTrimedString (s );
64+ // if (isArray) {
65+ // if (s.startsWith("\"")) {
66+ // s = s.substring(1);
67+ // }
68+ // if (s.endsWith("\"")) {
69+ // s = s.substring(0, s.length() - 1);
70+ // }
71+ // }
72+ return s ;//isJsonCorrect(s) ? s : null;
5573 }
5674
75+ /**json转JSONObject
76+ * @param json
77+ * @return
78+ */
79+ public static JSONObject parseObject (Object obj ) {
80+ return parseObject (toJSONString (obj ));
81+ }
5782 /**json转JSONObject
5883 * @param json
5984 * @return
@@ -72,11 +97,19 @@ public static JSONObject parseObject(String json, int features) {
7297 try {
7398 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), JSONObject .class , features );
7499 } catch (Exception e ) {
75- System . out . println (TAG + "parseObject catch \n " + e .getMessage ());
100+ Log . i (TAG , "parseObject catch \n " + e .getMessage ());
76101 }
77102 return null ;
78103 }
79104
105+ /**JSONObject转实体类
106+ * @param object
107+ * @param clazz
108+ * @return
109+ */
110+ public static <T > T parseObject (JSONObject object , Class <T > clazz ) {
111+ return parseObject (toJSONString (object ), clazz );
112+ }
80113 /**json转实体类
81114 * @param json
82115 * @param clazz
@@ -88,7 +121,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
88121 features |= Feature .OrderedField .getMask ();
89122 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
90123 } catch (Exception e ) {
91- System . out . println (TAG + "parseObject catch \n " + e .getMessage ());
124+ Log . i (TAG , "parseObject catch \n " + e .getMessage ());
92125 }
93126 return null ;
94127 }
@@ -98,7 +131,20 @@ public static <T> T parseObject(String json, Class<T> clazz) {
98131 * @return
99132 */
100133 public static JSONArray parseArray (String json ) {
101- return com .alibaba .fastjson .JSON .parseArray (json );
134+ try {
135+ return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ));
136+ } catch (Exception e ) {
137+ Log .i (TAG , "parseArray catch \n " + e .getMessage ());
138+ }
139+ return null ;
140+ }
141+ /**JSONArray转实体类列表
142+ * @param array
143+ * @param clazz
144+ * @return
145+ */
146+ public static <T > List <T > parseArray (JSONArray array , Class <T > clazz ) {
147+ return parseArray (toJSONString (array ), clazz );
102148 }
103149 /**json转实体类列表
104150 * @param json
@@ -107,9 +153,9 @@ public static JSONArray parseArray(String json) {
107153 */
108154 public static <T > List <T > parseArray (String json , Class <T > clazz ) {
109155 try {
110- return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json ), clazz );
156+ return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ), clazz );
111157 } catch (Exception e ) {
112- System . out . println (TAG + "parseArray catch \n " + e .getMessage ());
158+ Log . i (TAG , "parseArray catch \n " + e .getMessage ());
113159 }
114160 return null ;
115161 }
@@ -125,7 +171,7 @@ public static String toJSONString(Object obj) {
125171 try {
126172 return com .alibaba .fastjson .JSON .toJSONString (obj );
127173 } catch (Exception e ) {
128- System . out . println (TAG + "toJSONString catch \n " + e .getMessage ());
174+ Log . i (TAG , "toJSONString catch \n " + e .getMessage ());
129175 }
130176 return null ;
131177 }
@@ -142,7 +188,7 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
142188 try {
143189 return com .alibaba .fastjson .JSON .toJSONString (obj , features );
144190 } catch (Exception e ) {
145- System . out . println (TAG + "toJSONString catch \n " + e .getMessage ());
191+ Log . i (TAG , "toJSONString catch \n " + e .getMessage ());
146192 }
147193 return null ;
148194 }
0 commit comments