1414
1515package zuo .biao .apijson ;
1616
17+ import java .util .List ;
18+
1719import com .alibaba .fastjson .JSONArray ;
1820import com .alibaba .fastjson .JSONObject ;
1921import com .alibaba .fastjson .parser .Feature ;
2022import com .alibaba .fastjson .serializer .SerializerFeature ;
2123
22- import java .util .List ;
23-
2424/**阿里json封装类 防止解析时异常
2525 * @author Lemon
2626 */
@@ -34,8 +34,8 @@ public class JSON {
3434 public static boolean isJsonCorrect (String s ) {
3535 System .out .println (TAG + "isJsonCorrect <<<< " + s + " >>>>>>>" );
3636 if (s == null
37- // || s.equals("[]")
38- // || s.equals("{}")
37+ // || s.equals("[]")
38+ // || s.equals("{}")
3939 || s .equals ("" )
4040 || s .equals ("[null]" )
4141 || s .equals ("{null}" )
@@ -59,9 +59,17 @@ public static String getCorrectJson(String s) {
5959 * @return
6060 */
6161 public static JSONObject parseObject (String json ) {
62+ int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
63+ features |= Feature .OrderedField .getMask ();
64+ return parseObject (json , features );
65+ }
66+ /**json转JSONObject
67+ * @param json
68+ * @param features
69+ * @return
70+ */
71+ public static JSONObject parseObject (String json , int features ) {
6272 try {
63- int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
64- features |= Feature .SortFeidFastMatch .getMask ();
6573 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), JSONObject .class , features );
6674 } catch (Exception e ) {
6775 System .out .println (TAG + "parseObject catch \n " + e .getMessage ());
@@ -77,7 +85,7 @@ public static JSONObject parseObject(String json) {
7785 public static <T > T parseObject (String json , Class <T > clazz ) {
7886 try {
7987 int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
80- features |= Feature .SortFeidFastMatch .getMask ();
88+ features |= Feature .OrderedField .getMask ();
8189 return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
8290 } catch (Exception e ) {
8391 System .out .println (TAG + "parseObject catch \n " + e .getMessage ());
@@ -112,12 +120,34 @@ public static <T> List<T> parseArray(String json, Class<T> clazz) {
112120 */
113121 public static String toJSONString (Object obj ) {
114122 try {
115- return com .alibaba .fastjson .JSON .toJSONString (obj , SerializerFeature .SortField );
123+ return com .alibaba .fastjson .JSON .toJSONString (obj );
124+ } catch (Exception e ) {
125+ System .out .println (TAG + "toJSONString catch \n " + e .getMessage ());
126+ }
127+ return null ;
128+ }
129+
130+ /**实体类转json
131+ * @param obj
132+ * @param features
133+ * @return
134+ */
135+ public static String toJSONString (Object obj , SerializerFeature ... features ) {
136+ try {
137+ return com .alibaba .fastjson .JSON .toJSONString (obj , features );
116138 } catch (Exception e ) {
117139 System .out .println (TAG + "toJSONString catch \n " + e .getMessage ());
118140 }
119141 return null ;
120142 }
121143
144+ /**格式化,显示更好看
145+ * @param json
146+ * @return
147+ */
148+ public static String format (String json ) {
149+ return toJSONString (parseObject (json ), SerializerFeature .PrettyFormat );
150+ }
151+
122152
123153}
0 commit comments