1616
1717import java .util .List ;
1818
19+ import android .util .Log ;
20+
1921import com .alibaba .fastjson .JSONArray ;
2022import com .alibaba .fastjson .JSONObject ;
2123import com .alibaba .fastjson .parser .Feature ;
@@ -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,8 +52,24 @@ 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
5775 /**json转JSONObject
@@ -72,11 +90,19 @@ public static JSONObject parseObject(String json, int features) {
7290 try {
7391 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), JSONObject .class , features );
7492 } catch (Exception e ) {
75- System . out . println (TAG + "parseObject catch \n " + e .getMessage ());
93+ Log . i (TAG , "parseObject catch \n " + e .getMessage ());
7694 }
7795 return null ;
7896 }
7997
98+ /**JSONObject转实体类
99+ * @param object
100+ * @param clazz
101+ * @return
102+ */
103+ public static <T > T parseObject (JSONObject object , Class <T > clazz ) {
104+ return parseObject (toJSONString (object ), clazz );
105+ }
80106 /**json转实体类
81107 * @param json
82108 * @param clazz
@@ -88,7 +114,7 @@ public static <T> T parseObject(String json, Class<T> clazz) {
88114 features |= Feature .OrderedField .getMask ();
89115 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
90116 } catch (Exception e ) {
91- System . out . println (TAG + "parseObject catch \n " + e .getMessage ());
117+ Log . i (TAG , "parseObject catch \n " + e .getMessage ());
92118 }
93119 return null ;
94120 }
@@ -98,7 +124,20 @@ public static <T> T parseObject(String json, Class<T> clazz) {
98124 * @return
99125 */
100126 public static JSONArray parseArray (String json ) {
101- return com .alibaba .fastjson .JSON .parseArray (json );
127+ try {
128+ return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ));
129+ } catch (Exception e ) {
130+ Log .i (TAG , "parseArray catch \n " + e .getMessage ());
131+ }
132+ return null ;
133+ }
134+ /**JSONArray转实体类列表
135+ * @param array
136+ * @param clazz
137+ * @return
138+ */
139+ public static <T > List <T > parseArray (JSONArray array , Class <T > clazz ) {
140+ return parseArray (toJSONString (array ), clazz );
102141 }
103142 /**json转实体类列表
104143 * @param json
@@ -107,9 +146,9 @@ public static JSONArray parseArray(String json) {
107146 */
108147 public static <T > List <T > parseArray (String json , Class <T > clazz ) {
109148 try {
110- return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json ), clazz );
149+ return com .alibaba .fastjson .JSON .parseArray (getCorrectJson (json , true ), clazz );
111150 } catch (Exception e ) {
112- System . out . println (TAG + "parseArray catch \n " + e .getMessage ());
151+ Log . i (TAG , "parseArray catch \n " + e .getMessage ());
113152 }
114153 return null ;
115154 }
@@ -119,10 +158,13 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
119158 * @return
120159 */
121160 public static String toJSONString (Object obj ) {
161+ if (obj instanceof String ) {
162+ return (String ) obj ;
163+ }
122164 try {
123165 return com .alibaba .fastjson .JSON .toJSONString (obj );
124166 } catch (Exception e ) {
125- System . out . println (TAG + "toJSONString catch \n " + e .getMessage ());
167+ Log . i (TAG , "toJSONString catch \n " + e .getMessage ());
126168 }
127169 return null ;
128170 }
@@ -133,10 +175,13 @@ public static String toJSONString(Object obj) {
133175 * @return
134176 */
135177 public static String toJSONString (Object obj , SerializerFeature ... features ) {
178+ if (obj instanceof String ) {
179+ return (String ) obj ;
180+ }
136181 try {
137182 return com .alibaba .fastjson .JSON .toJSONString (obj , features );
138183 } catch (Exception e ) {
139- System . out . println (TAG + "toJSONString catch \n " + e .getMessage ());
184+ Log . i (TAG , "toJSONString catch \n " + e .getMessage ());
140185 }
141186 return null ;
142187 }
@@ -146,7 +191,14 @@ public static String toJSONString(Object obj, SerializerFeature... features) {
146191 * @return
147192 */
148193 public static String format (String json ) {
149- return toJSONString (parseObject (json ), SerializerFeature .PrettyFormat );
194+ return format (parseObject (json ));
195+ }
196+ /**格式化,显示更好看
197+ * @param object
198+ * @return
199+ */
200+ public static String format (JSONObject object ) {
201+ return toJSONString (object , SerializerFeature .PrettyFormat );
150202 }
151203
152204
0 commit comments