1- package zuo . biao . apijson ;
1+ /*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)
22
3- import java .util .List ;
3+ Licensed under the Apache License, Version 2.0 (the "License");
4+ you may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+
7+ http://www.apache.org/licenses/LICENSE-2.0
8+
9+ Unless required by applicable law or agreed to in writing, software
10+ distributed under the License is distributed on an "AS IS" BASIS,
11+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ See the License for the specific language governing permissions and
13+ limitations under the License.*/
414
15+ package zuo .biao .apijson ;
16+
17+ import com .alibaba .fastjson .JSONArray ;
518import com .alibaba .fastjson .JSONObject ;
619import com .alibaba .fastjson .parser .Feature ;
720import com .alibaba .fastjson .serializer .SerializerFeature ;
821
22+ import java .util .List ;
23+
924/**阿里json封装类 防止解析时异常
10- * @author Young
11- * @modifier Lemon
25+ * @author Lemon
1226 */
1327public class JSON {
14- private static final String TAG = "Json " ;
28+ private static final String TAG = "JSON " ;
1529
1630 /**判断json格式是否正确
1731 * @param s
@@ -35,58 +49,70 @@ public static String getCorrectJson(String s) {
3549 return isJsonCorrect (s ) ? s : "" ;
3650 }
3751
38-
39- // parseObject(String text, Class<PersonalInfoDO> clazz)
40-
41- public static JSONObject parseObject (String s ) {
52+ /**json转JSONObject
53+ * @param json
54+ * @return
55+ */
56+ public static JSONObject parseObject (String json ) {
4257 try {
4358 int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
4459 features |= Feature .SortFeidFastMatch .getMask ();
45- return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (s ), JSONObject .class , features );
60+ return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), JSONObject .class , features );
4661 } catch (Exception e ) {
4762 System .out .println (TAG + "parseObject catch \n " + e .getMessage ());
4863 }
4964 return null ;
5065 }
5166
52- public static <T > T parseObject (String s , Class <T > clazz ) {
67+ /**json转实体类
68+ * @param json
69+ * @param clazz
70+ * @return
71+ */
72+ public static <T > T parseObject (String json , Class <T > clazz ) {
5373 try {
5474 int features = com .alibaba .fastjson .JSON .DEFAULT_PARSER_FEATURE ;
5575 features |= Feature .SortFeidFastMatch .getMask ();
56- return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (s ), clazz , features );
76+ return com .alibaba .fastjson .JSON .parseObject (getCorrectJson (json ), clazz , features );
5777 } catch (Exception e ) {
5878 System .out .println (TAG + "parseObject catch \n " + e .getMessage ());
5979 }
6080 return null ;
6181 }
6282
63- // com.alibaba.fastjson.JSON.toJSONString
64- public static String toJSONString (Object obj ) {
83+ /**json转JSONArray
84+ * @param json
85+ * @return
86+ */
87+ public static JSONArray parseArray (String json ) {
88+ return com .alibaba .fastjson .JSON .parseArray (json );
89+ }
90+ /**json转实体类列表
91+ * @param json
92+ * @param clazz
93+ * @return
94+ */
95+ public static <T > List <T > parseArray (String json , Class <T > clazz ) {
6596 try {
66- return com .alibaba .fastjson .JSON .toJSONString ( obj , SerializerFeature . SortField );
97+ return com .alibaba .fastjson .JSON .parseArray ( getCorrectJson ( json ), clazz );
6798 } catch (Exception e ) {
68- System .out .println (TAG + "toJSONString catch \n " + e .getMessage ());
99+ System .out .println (TAG + "parseArray catch \n " + e .getMessage ());
69100 }
70101 return null ;
71102 }
72103
73- // // com.alibaba.fastjson.JSON.toJSONString
74- // public static <T> String toJSONString(List<T> list ){
75- //
76- // try {
77- // return com.alibaba.fastjson.JSON.toJSONString(list);
78- // } catch (Exception e) {
79- // }
80- // return null;
81- // }
82- // com.alibaba.fastjson.JSON.parseArray(json, NearbyCraftsManDO.class);
83- public static <T > List <T > parseArray (String s , Class <T > clazz ) {
104+ /**实体类转json
105+ * @param obj
106+ * @return
107+ */
108+ public static String toJSONString (Object obj ) {
84109 try {
85- return com .alibaba .fastjson .JSON .parseArray ( getCorrectJson ( s ), clazz );
110+ return com .alibaba .fastjson .JSON .toJSONString ( obj , SerializerFeature . SortField );
86111 } catch (Exception e ) {
87- System .out .println (TAG + "parseArray catch \n " + e .getMessage ());
112+ System .out .println (TAG + "toJSONString catch \n " + e .getMessage ());
88113 }
89114 return null ;
90115 }
91116
117+
92118}
0 commit comments