Administrator
2023-04-21 195945efc5db921a4c9eb8cf9421c172273293f5
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3
4
5 import java.lang.reflect.Field;
6 import java.sql.Timestamp;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9
10 import org.apache.commons.lang3.StringUtils;
11
12 import com.google.gson.JsonArray;
13 import com.google.gson.JsonElement;
14 import com.google.gson.JsonObject;
15
16
17 public class JSONobj {
18     public static JsonObject getJsonObject(JsonObject jo,String n)
19     {
20         if(jo==null||jo.isJsonNull())
21             return null;
22         JsonElement je=jo.get(n);
23         if(je==null||je.isJsonNull())
24             return null;
25         JsonObject t=null;
26         try{
27             t=je.getAsJsonObject();
28         }catch (Exception e) {
29         }
30         
31         return t;
32     }
33     public static JsonArray getJsonArray(JsonObject jo,String n)
34     {
35         if(jo==null||jo.isJsonNull())
36             return null;
37         JsonElement je=jo.get(n);
38         if(je==null||je.isJsonNull())
39             return null;
40         JsonArray t=null;
41         try{
42             t=je.getAsJsonArray();
43         }catch (Exception e) {
44         }        
45         return t;
46     }
47     public static Long getlong(JsonObject jo,String n)
48     {
49         if(jo==null||jo.isJsonNull())
50             return 0L;
51         JsonElement je=jo.get(n);
52         if(je==null||je.isJsonNull())
53             return 0L;
54         long t=0;
55         try{
56             t=je.getAsLong();
57         }catch (Exception e) {
58         }
59         
60         return t;
61     }
62     public static Double getdouble(JsonObject jo,String n)
63     {
64         if(jo==null||jo.isJsonNull())
65             return 0.0;
66         JsonElement je=jo.get(n);
67         if(je==null||je.isJsonNull())
68             return 0.0;
69         double t=0;
70         try{
71             t=je.getAsDouble();
72         }catch (Exception e) {
73         }        
74         return t;
75     }
76     public static Date getdate(JsonObject jo,String n)
77     {
78         if(jo==null||jo.isJsonNull())
79             return null;
80         JsonElement je=jo.get(n);
81         if(je==null||je.isJsonNull())
82             return null;
83         Date t=null;
84         String tt=JSONobj.getstring(jo, n);
85         if(tt!=null)
86             tt=StringUtils.trimToEmpty(tt);
87         try{
88             SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
89             t=sdf.parse(tt);
90         }catch (Exception e) {
91             SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
92             try{
93                 t=sdf1.parse(tt);
94             }catch(Exception e1){
95             }
96         }        
97         return t;
98     }
99     public static Integer getint(JsonObject jo,String n)
100     {
101         if(jo==null||jo.isJsonNull())
102             return null;
103         JsonElement je=jo.get(n);
104         if(je==null||je.isJsonNull())
105             return null;
106         int t=0;
107         try{
108             t=je.getAsInt();
109         }catch (Exception e) {
110         }
111         return t;
112     }
113     public static boolean getbool(JsonObject jo,String n)
114     {
115         if(jo==null||jo.isJsonNull())
116             return false;
117         JsonElement je=jo.get(n);
118         if(je==null||je.isJsonNull())
119             return false;
120         boolean t=false;
121         try{
122             String tt=je.getAsString();
123             if(tt==null)
124                 return false;
125             if(tt!=null)
126                 tt=StringUtils.trimToEmpty(tt);
127             if("1".equals(tt)||"true".equals(tt))
128             {
129                 t=true;
130             }
131         }catch (Exception e) {
132         }
133         return t;
134     }
135     public static String getstring(JsonObject jo,String n)
136     {
137         if(jo==null||jo.isJsonNull())
138             return null;
139         JsonElement je=jo.get(n);
140         if(je==null||je.isJsonNull())
141             return null;
142         String tt=null;
143         try{
144             tt=je.getAsString();
145             if(tt!=null)
146                 tt=StringUtils.trimToEmpty(tt);
147         }catch (Exception e) {
148             e.printStackTrace();
149             System.out.println("get string error:"+n);
150         }
151         return tt;
152     }
153     public static String getstringnonull(JsonObject jo,String n)
154     {
155         if(jo==null||jo.isJsonNull())
156             return "";
157         JsonElement je=jo.get(n);
158         if(je==null||je.isJsonNull())
159             return "";
160         String tt="";
161         try{
162             tt=je.getAsString();
163             if(tt!=null)
164                 tt=StringUtils.trimToEmpty(tt);
165         }catch (Exception e) {
166         }
167         return tt;
168     }
169     /**
170      * timestamp/datetime
171      * @param jo
172      * @param n
173      * @return
174      */
175     public static String gettmstring(JsonObject jo,String n)
176     {
177         if(jo==null||jo.isJsonNull())
178             return null;
179         JsonElement je=jo.get(n);
180         if(je==null||je.isJsonNull())
181             return null;
182         String tt="1970-01-01 08:00:00";
183         try{
184             tt=je.getAsString();
185             if(tt!=null)
186                 tt=StringUtils.trimToEmpty(tt);
187         }catch (Exception e) {
188 //            System.out.println("get string error:"+n);
189         }
190         return tt;
191     }
192     /**
193      * date
194      * @param jo
195      * @param n
196      * @return
197      */
198     public static String getdtstring(JsonObject jo,String n)
199     {
200         if(jo==null||jo.isJsonNull())
201             return null;
202         JsonElement je=jo.get(n);
203         if(je==null||je.isJsonNull())
204             return null;
205         String tt="1970-01-01";
206         try{
207             tt=je.getAsString();
208             if(tt!=null)
209                 tt=StringUtils.trimToEmpty(tt);
210         }catch (Exception e) {
211 //            System.out.println("get string error:"+n);
212         }
213         return tt;
214     }
215     
216     public static <T> T getobj(JsonObject jo,T obj)
217     {
218         if(jo==null||jo.isJsonNull())
219             return null;
220         @SuppressWarnings("rawtypes")
221         Class cls=obj.getClass();
222         try {
223             Field[] fs=cls.getDeclaredFields();
224             for (Field field : fs) {
225                 field.setAccessible(true);
226                 String fn=field.getName();
227                 if(field.getType()==String.class)
228                 {
229                     String t=JSONobj.getstring(jo, fn);
230                     if(t!=null)
231                         field.set(obj, t);
232                 }else if(field.getType()==Timestamp.class)
233                 {
234                     Date d=JSONobj.getdate(jo, fn);
235                     if(d!=null)
236                     {
237                         Timestamp tt=new Timestamp(d.getTime());
238                         field.set(obj, tt);
239                     }
240                 }else if(field.getType()==Date.class)
241                 {
242                     Date dt=JSONobj.getdate(jo, fn);
243                     if(dt!=null)
244                         field.set(obj, dt);
245                 }else if(field.getType()==int.class||field.getType()==Integer.class)
246                 {
247                     Integer io=JSONobj.getint(jo, fn);
248                     if(io!=null)
249                     {
250                         int i=io;
251                         field.setInt(obj, i);
252                     }
253                 }else if(field.getType()==double.class||field.getType()==Double.class)
254                 {
255                     Double d=JSONobj.getdouble(jo, fn);
256                     if(d!=null)
257                     {
258                         double dd=d;
259                         field.setDouble(obj, dd);    
260                     }
261                 }else if(field.getType()==long.class||field.getType()==Long.class)
262                 {
263                     Long l=JSONobj.getlong(jo, fn);
264                     if(l!=null)
265                     {
266                         long ll=l;
267                         field.setLong(obj, ll);
268                     }
269                 }else if(field.getType()==Boolean.class||field.getType()==boolean.class)
270                 {
271                     Boolean b=JSONobj.getbool(jo, fn);
272                     field.setBoolean(obj, b);
273                 }
274             }
275         } catch (IllegalAccessException e) {
276             e.printStackTrace();
277         }
278         return obj;
279     }
280     
281     public static <T> T getobjbyJsonobj(JsonObject jo,T obj)
282     {
283         if(jo==null)
284             return null;
285         @SuppressWarnings("rawtypes")
286         
287         Class cls=obj.getClass();
288         try {
289             Field[] fs=cls.getDeclaredFields();
290             for (Field field : fs) {
291                 field.setAccessible(true);
292                 String fn=field.getName();
293                 if(field.getType()==String.class)
294                 {
295                     String t=JSONobj.getstring(jo, fn);
296                     if(t!=null)
297                         field.set(obj, t);
298                 }else if(field.getType()==Timestamp.class)
299                 {
300                     Date d=JSONobj.getdate(jo, fn);
301                     if(d!=null)
302                     {
303                         Timestamp tt=new Timestamp(d.getTime());
304                         field.set(obj, tt);
305                     }
306                 }else if(field.getType()==Date.class)
307                 {
308                     Date dt=JSONobj.getdate(jo, fn);
309                     if(dt!=null)
310                         field.set(obj, dt);
311                 }else if(field.getType()==int.class||field.getType()==Integer.class)
312                 {
313                     Integer io=JSONobj.getint(jo, fn);
314                     if(io!=null)
315                     {
316                         int i=io;
317                         field.setInt(obj, i);
318                     }
319                 }else if(field.getType()==double.class||field.getType()==Double.class)
320                 {
321                     Double d=JSONobj.getdouble(jo, fn);
322                     if(d!=null)
323                     {
324                         double dd=d;
325                         field.setDouble(obj, dd);    
326                     }
327                 }else if(field.getType()==long.class||field.getType()==Long.class)
328                 {
329                     Long l=JSONobj.getlong(jo, fn);
330                     if(l!=null)
331                     {
332                         long ll=l;
333                         field.setLong(obj, ll);
334                     }
335                 }else if(field.getType()==Boolean.class||field.getType()==boolean.class)
336                 {
337                     Boolean b=JSONobj.getbool(jo, fn);
338                     field.setBoolean(obj, b);
339                 }
340             }
341         } catch (IllegalAccessException e) {
342             e.printStackTrace();
343         }
344         return obj;
345     }
346     
347 }