激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java實(shí)現(xiàn)操作JSON的便捷工具類完整實(shí)例【重寫Google的Gson】

Java實(shí)現(xiàn)操作JSON的便捷工具類完整實(shí)例【重寫Google的Gson】

2021-01-28 12:31歲寒松柏 Java教程

這篇文章主要介紹了Java實(shí)現(xiàn)操作JSON的便捷工具類,基于重寫Google的Gson實(shí)現(xiàn),涉及java針對(duì)json數(shù)據(jù)的各種常見轉(zhuǎn)換操作實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)操作JSON的便捷工具類。分享給大家供大家參考,具體如下:

對(duì)于JSON數(shù)據(jù)格式的處理,自開發(fā)Java以來,已用過多種JSON的開源工具,用得最好,也用得最High的恐怕要屬GoogleGson了。

特別為它寫了一個(gè)工具類,放入常備工具中,方便使用。下面是為GSON 1.5版本重寫的工具類。

依賴包:

slf4j-api-1.6.0.jar
slf4j-log4j12-1.6.0.jar
log4j-1.2.15.jar
gson-1.5.jar

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
 * Copyright 2010 Fuchun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.tools;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang.StringUtils;
/**
 * 包含操作 {@code JSON} 數(shù)據(jù)的常用方法的工具類。
 * <p />
 * 該工具類使用的 {@code JSON} 轉(zhuǎn)換引擎是 <a href="http://code.google.com/p/google-gson/" rel="external nofollow" rel="external nofollow" mce_href="http://code.google.com/p/google-gson/" rel="external nofollow" rel="external nofollow" target="_blank">
 * {@code Google Gson}</a>。 下面是工具類的使用案例:
 *
 * <pre>
 * public class User {
 *   @SerializedName("pwd")
 *   private String password;
 *   @Expose
 *   @SerializedName("uname")
 *   private String username;
 *   @Expose
 *   @Since(1.1)
 *   private String gender;
 *   @Expose
 *   @Since(1.0)
 *   private String sex;
 *
 *   public User() {}
 *   public User(String username, String password, String gender) {
 *     // user constructor code... ... ...
 *   }
 *
 *   public String getUsername()
 *   ... ... ...
 * }
 * List<User> userList = new LinkedList<User>();
 * User jack = new User("Jack", "123456", "Male");
 * User marry = new User("Marry", "888888", "Female");
 * userList.add(jack);
 * userList.add(marry);
 * Type targetType = new TypeToken<List<User>>(){}.getType();
 * String sUserList1 = JSONUtils.toJson(userList, targetType);
 * sUserList1 ----> [{"uname":"jack","gender":"Male","sex":"Male"},{"uname":"marry","gender":"Female","sex":"Female"}]
 * String sUserList2 = JSONUtils.toJson(userList, targetType, false);
 * sUserList2 ----> [{"uname":"jack","pwd":"123456","gender":"Male","sex":"Male"},{"uname":"marry","pwd":"888888","gender":"Female","sex":"Female"}]
 * String sUserList3 = JSONUtils.toJson(userList, targetType, 1.0d, true);
 * sUserList3 ----> [{"uname":"jack","sex":"Male"},{"uname":"marry","sex":"Female"}]
 * </pre>
 *
 * @author Fuchun
 * @since ay-commons-lang 1.0
 * @version 1.1.0
 */
public class JSONUtils {
  private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class);
  /** 空的 {@code JSON} 數(shù)據(jù) - <code>"{}"</code>。 */
  public static final String EMPTY_JSON = "{}";
  /** 空的 {@code JSON} 數(shù)組(集合)數(shù)據(jù) - {@code "[]"}。 */
  public static final String EMPTY_JSON_ARRAY = "[]";
  /** 默認(rèn)的 {@code JSON} 日期/時(shí)間字段的格式化模式。 */
  public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";
  /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本號(hào)常量 - {@code 1.0}。 */
  public static final double SINCE_VERSION_10 = 1.0d;
  /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本號(hào)常量 - {@code 1.1}。 */
  public static final double SINCE_VERSION_11 = 1.1d;
  /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本號(hào)常量 - {@code 1.2}。 */
  public static final double SINCE_VERSION_12 = 1.2d;
  /** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本號(hào)常量 - {@code 1.0}。 */
  public static final double UNTIL_VERSION_10 = SINCE_VERSION_10;
  /** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本號(hào)常量 - {@code 1.1}。 */
  public static final double UNTIL_VERSION_11 = SINCE_VERSION_11;
  /** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本號(hào)常量 - {@code 1.2}。 */
  public static final double UNTIL_VERSION_12 = SINCE_VERSION_12;
  /**
   * <p>
   * <code>JSONUtils</code> instances should NOT be constructed in standard programming. Instead,
   * the class should be used as <code>JSONUtils.fromJson("foo");</code>.
   * </p>
   * <p>
   * This constructor is public to permit tools that require a JavaBean instance to operate.
   * </p>
   */
  public JSONUtils() {
    super();
  }
  /**
   * 將給定的目標(biāo)對(duì)象根據(jù)指定的條件參數(shù)轉(zhuǎn)換成 {@code JSON} 格式的字符串。
   * <p />
   * <strong>該方法轉(zhuǎn)換發(fā)生錯(cuò)誤時(shí),不會(huì)拋出任何異常。若發(fā)生錯(cuò)誤時(shí),曾通對(duì)象返回 <code>"{}"</code>; 集合或數(shù)組對(duì)象返回 <code>"[]"</code>
   * </strong>
   *
   * @param target 目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @param isSerializeNulls 是否序列化 {@code null} 值字段。
   * @param version 字段的版本號(hào)注解。
   * @param datePattern 日期字段的格式化模式。
   * @param excludesFieldsWithoutExpose 是否排除未標(biāo)注 {@literal @Expose} 注解的字段。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
      String datePattern, boolean excludesFieldsWithoutExpose) {
    if (target == null) return EMPTY_JSON;
    GsonBuilder builder = new GsonBuilder();
    if (isSerializeNulls) builder.serializeNulls();
    if (version != null) builder.setVersion(version.doubleValue());
    if (StringUtils.isBlank(datePattern)) datePattern = DEFAULT_DATE_PATTERN;
    builder.setDateFormat(datePattern);
    if (excludesFieldsWithoutExpose) builder.excludeFieldsWithoutExposeAnnotation();
    return toJson(target, targetType, builder);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法只用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   * <ul>
   * <li>該方法只會(huì)轉(zhuǎn)換標(biāo)有 {@literal @Expose} 注解的字段;</li>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法會(huì)轉(zhuǎn)換所有未標(biāo)注或已標(biāo)注 {@literal @Since} 的字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target) {
    return toJson(target, null, false, null, null, true);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法只用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   * <ul>
   * <li>該方法只會(huì)轉(zhuǎn)換標(biāo)有 {@literal @Expose} 注解的字段;</li>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法會(huì)轉(zhuǎn)換所有未標(biāo)注或已標(biāo)注 {@literal @Since} 的字段;</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param datePattern 日期字段的格式化模式。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, String datePattern) {
    return toJson(target, null, false, null, datePattern, true);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法只用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   * <ul>
   * <li>該方法只會(huì)轉(zhuǎn)換標(biāo)有 {@literal @Expose} 注解的字段;</li>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param version 字段的版本號(hào)注解({@literal @Since})。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Double version) {
    return toJson(target, null, false, version, null, true);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法只用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   * <ul>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法會(huì)轉(zhuǎn)換所有未標(biāo)注或已標(biāo)注 {@literal @Since} 的字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param excludesFieldsWithoutExpose 是否排除未標(biāo)注 {@literal @Expose} 注解的字段。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, boolean excludesFieldsWithoutExpose) {
    return toJson(target, null, false, null, null, excludesFieldsWithoutExpose);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法只用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   * <ul>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param version 字段的版本號(hào)注解({@literal @Since})。
   * @param excludesFieldsWithoutExpose 是否排除未標(biāo)注 {@literal @Expose} 注解的字段。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Double version, boolean excludesFieldsWithoutExpose) {
    return toJson(target, null, false, version, null, excludesFieldsWithoutExpose);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法通常用來轉(zhuǎn)換使用泛型的對(duì)象。</strong>
   * <ul>
   * <li>該方法只會(huì)轉(zhuǎn)換標(biāo)有 {@literal @Expose} 注解的字段;</li>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法會(huì)轉(zhuǎn)換所有未標(biāo)注或已標(biāo)注 {@literal @Since} 的字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Type targetType) {
    return toJson(target, targetType, false, null, null, true);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法通常用來轉(zhuǎn)換使用泛型的對(duì)象。</strong>
   * <ul>
   * <li>該方法只會(huì)轉(zhuǎn)換標(biāo)有 {@literal @Expose} 注解的字段;</li>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @param version 字段的版本號(hào)注解({@literal @Since})。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Type targetType, Double version) {
    return toJson(target, targetType, false, version, null, true);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法通常用來轉(zhuǎn)換使用泛型的對(duì)象。</strong>
   * <ul>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法會(huì)轉(zhuǎn)換所有未標(biāo)注或已標(biāo)注 {@literal @Since} 的字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @param excludesFieldsWithoutExpose 是否排除未標(biāo)注 {@literal @Expose} 注解的字段。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Type targetType, boolean excludesFieldsWithoutExpose) {
    return toJson(target, targetType, false, null, null, excludesFieldsWithoutExpose);
  }
  /**
   * 將給定的目標(biāo)對(duì)象轉(zhuǎn)換成 {@code JSON} 格式的字符串。<strong>此方法通常用來轉(zhuǎn)換使用泛型的對(duì)象。</strong>
   * <ul>
   * <li>該方法不會(huì)轉(zhuǎn)換 {@code null} 值字段;</li>
   * <li>該方法轉(zhuǎn)換時(shí)使用默認(rèn)的 日期/時(shí)間 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
   * </ul>
   *
   * @param target 要轉(zhuǎn)換成 {@code JSON} 的目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @param version 字段的版本號(hào)注解({@literal @Since})。
   * @param excludesFieldsWithoutExpose 是否排除未標(biāo)注 {@literal @Expose} 注解的字段。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.0
   */
  public static String toJson(Object target, Type targetType, Double version, boolean excludesFieldsWithoutExpose) {
    return toJson(target, targetType, false, version, null, excludesFieldsWithoutExpose);
  }
  /**
   * 將給定的 {@code JSON} 字符串轉(zhuǎn)換成指定的類型對(duì)象。
   *
   * @param <T> 要轉(zhuǎn)換的目標(biāo)類型。
   * @param json 給定的 {@code JSON} 字符串。
   * @param token {@code com.google.gson.reflect.TypeToken} 的類型指示類對(duì)象。
   * @param datePattern 日期格式模式。
   * @return 給定的 {@code JSON} 字符串表示的指定的類型對(duì)象。
   * @since 1.0
   */
  public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {
    if (StringUtils.isBlank(json)) {
      return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (StringUtils.isBlank(datePattern)) {
      datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
      return gson.fromJson(json, token.getType());
    } catch (Exception ex) {
      LOGGER.error(json + " 無法轉(zhuǎn)換為 " + token.getRawType().getName() + " 對(duì)象!", ex);
      return null;
    }
  }
  /**
   * 將給定的 {@code JSON} 字符串轉(zhuǎn)換成指定的類型對(duì)象。
   *
   * @param <T> 要轉(zhuǎn)換的目標(biāo)類型。
   * @param json 給定的 {@code JSON} 字符串。
   * @param token {@code com.google.gson.reflect.TypeToken} 的類型指示類對(duì)象。
   * @return 給定的 {@code JSON} 字符串表示的指定的類型對(duì)象。
   * @since 1.0
   */
  public static <T> T fromJson(String json, TypeToken<T> token) {
    return fromJson(json, token, null);
  }
  /**
   * 將給定的 {@code JSON} 字符串轉(zhuǎn)換成指定的類型對(duì)象。<strong>此方法通常用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   *
   * @param <T> 要轉(zhuǎn)換的目標(biāo)類型。
   * @param json 給定的 {@code JSON} 字符串。
   * @param clazz 要轉(zhuǎn)換的目標(biāo)類。
   * @param datePattern 日期格式模式。
   * @return 給定的 {@code JSON} 字符串表示的指定的類型對(duì)象。
   * @since 1.0
   */
  public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
    if (StringUtils.isBlank(json)) {
      return null;
    }
    GsonBuilder builder = new GsonBuilder();
    if (StringUtils.isBlank(datePattern)) {
      datePattern = DEFAULT_DATE_PATTERN;
    }
    Gson gson = builder.create();
    try {
      return gson.fromJson(json, clazz);
    } catch (Exception ex) {
      LOGGER.error(json + " 無法轉(zhuǎn)換為 " + clazz.getName() + " 對(duì)象!", ex);
      return null;
    }
  }
  /**
   * 將給定的 {@code JSON} 字符串轉(zhuǎn)換成指定的類型對(duì)象。<strong>此方法通常用來轉(zhuǎn)換普通的 {@code JavaBean} 對(duì)象。</strong>
   *
   * @param <T> 要轉(zhuǎn)換的目標(biāo)類型。
   * @param json 給定的 {@code JSON} 字符串。
   * @param clazz 要轉(zhuǎn)換的目標(biāo)類。
   * @return 給定的 {@code JSON} 字符串表示的指定的類型對(duì)象。
   * @since 1.0
   */
  public static <T> T fromJson(String json, Class<T> clazz) {
    return fromJson(json, clazz, null);
  }
  /**
   * 將給定的目標(biāo)對(duì)象根據(jù){@code GsonBuilder} 所指定的條件參數(shù)轉(zhuǎn)換成 {@code JSON} 格式的字符串。
   * <p />
   * 該方法轉(zhuǎn)換發(fā)生錯(cuò)誤時(shí),不會(huì)拋出任何異常。若發(fā)生錯(cuò)誤時(shí),{@code JavaBean} 對(duì)象返回 <code>"{}"</code>; 集合或數(shù)組對(duì)象返回
   * <code>"[]"</code>。 其本基本類型,返回相應(yīng)的基本值。
   *
   * @param target 目標(biāo)對(duì)象。
   * @param targetType 目標(biāo)對(duì)象的類型。
   * @param builder 可定制的{@code Gson} 構(gòu)建器。
   * @return 目標(biāo)對(duì)象的 {@code JSON} 格式的字符串。
   * @since 1.1
   */
  public static String toJson(Object target, Type targetType, GsonBuilder builder) {
    if (target == null) return EMPTY_JSON;
    Gson gson = null;
    if (builder == null) {
      gson = new Gson();
    } else {
      gson = builder.create();
    }
    String result = EMPTY_JSON;
    try {
      if (targetType == null) {
        result = gson.toJson(target);
      } else {
        result = gson.toJson(target, targetType);
      }
    } catch (Exception ex) {
      LOGGER.warn("目標(biāo)對(duì)象 " + target.getClass().getName() + " 轉(zhuǎn)換 JSON 字符串時(shí),發(fā)生異常!", ex);
      if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?>
          || target.getClass().isArray()) {
        result = EMPTY_JSON_ARRAY;
      }
    }
    return result;
  }
}

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

原文鏈接:http://blog.csdn.net/xusongsong520/article/details/7941847

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩高清一区 | 91精彩视频| 欧美成人三级视频 | 一级做受毛片免费大片 | 黄视频免费在线观看 | 日本黄免费 | 制服丝袜日日夜夜 | 久久蜜臀一区二区三区av | 国产精品久久久久无码av | 伊人网站 | 亚洲午夜在线视频 | 国产精品视频一区二区三区综合 | 精品一区二区电影 | 毛片118极品美女写真 | 深夜免费视频 | 国产www免费| 天天草天天干天天 | 精品国产91一区二区三区 | 7m视频成人精品分类 | 亚洲精品久久久久久久久久 | 一级片999 | 精国产品一区二区三区四季综 | 日韩欧美激情视频 | 男女一边摸一边做羞羞视频免费 | 草久网 | 亚洲欧美日韩精品久久 | 欧美一级毛片欧美一级成人毛片 | 一本色道久久综合狠狠躁篇适合什么人看 | 嗯~啊~弄嗯~啊h高潮视频 | 中文字幕国产日韩 | 自拍偷拍亚洲图片 | 最新黄色av| 欧美国产第一页 | 欧美成人免费看 | 在线亚洲播放 | 久在线观看福利视频69 | h色在线观看 | 亚洲五码在线观看视频 | 国产亚洲黑人性受xxxx精品 | 亚洲最新色 | 精品一区二区中文字幕 |