public class GsonUtils {/** * 将Json数据解析成相应的映射对象 * * @param jsonData * @param type * @param <T> * @return */ public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {T result = null;if (!TextUtils.isEmpty(jsonData)) {Gson gson = new GsonBuilder().create();try {result = gson.fromJson(jsonData, type);} catch (Exception e) {e.printStackTrace();} finally {if (gson != null) {gson = null;}}}return result;}/** * 将Json数组解析成相应的映射对象List * * @param jsonData * @param type * @param <T> * @return */ public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> type) {List<T> result = null;if (!TextUtils.isEmpty(jsonData)) {Gson gson = new GsonBuilder().create();try {JsonParser parser = new JsonParser();JsonArray Jarray = parser.parse(jsonData).getAsJsonArray();if (Jarray != null) {result = new ArrayList<>();for (JsonElement obj : Jarray) {try {T cse = gson.fromJson(obj, type);result.add(cse);} catch (Exception e) {e.printStackTrace();}}}} catch (Exception e) {e.printStackTrace();} finally {if (gson != null) {gson = null;}}}return result;}/** * 将对象转换成Json * * @param entity * @param <T> * @return */ public static <T> String toJsonWithSerializeNulls(T entity) {entity.getClass();Gson gson = new GsonBuilder().serializeNulls().create();String result = "";try {result = gson.toJson(entity);} catch (Exception e) {e.printStackTrace();} finally {if (gson != null) {gson = null;}}return result;}/** * 将list排除值为null的字段转换成Json数组 * * @param list * @param <T> * @return */ public static <T> String toJsonArrayWithSerializeNulls(List<T> list) {Gson gson = new GsonBuilder().serializeNulls().create();String result = "";try {result = gson.toJson(list);} catch (Exception e) {e.printStackTrace();} finally {if (gson != null) {gson = null;}}return result;}/** * 将list中将Expose注解的字段转换成Json数组 * * @param list * @param <T> * @return */ public static <T> String toJsonArrayWithExpose(List<T> list) {Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();String result = "";try {result = gson.toJson(list);} catch (Exception e) {e.printStackTrace();} finally {if (gson != null) {gson = null;}}return result;}}
详细解决方案
GSON封装Utils
热度:20 发布时间:2023-10-12 04:35:54.0
相关解决方案
- Gson 解析json 字符串的有关问题
- [求助]import utils.Complex;
- 比较 fastjson gson
- Gson json to list or 地图
- 适用的Json工具类(Gson)
- Gson MalformedJsonException,该怎么解决
- com.nfschina.utils.DataPage 这属于哪个jar包?该怎么解决
- android 代码搅混有关 gson
- 使用 GSON 将动态字段反序列化为 POJO
- Gson Library写入文件,仅更改特定值
- 有没有办法在 GSON 中使用一个类作为另一个类的变量类型?
- Gson string bean对象转化工具类
- vuex3源码注释系列 /res/utils.js
- JSON(二)——JSON转换(GSON)
- java.utils.concurrent 新类库中的构件(一) CountDownLatch
- 深度学习实战(十四)——Tensorflow Object Detection API 源码分析之 utils/label_map_util.py
- 关于使用django 首次创建超级管理员时,出现 django.db.utils.OperationalError: no such table: auth_user 错误
- CS0433: 类型“Utils”同时存在于 App_Code.dll和WebApp.dll中
- Python - Django 执行 migrate 操作时异常: django.db.utils.OperationalError: (1051, “Unknown table ‘xxx‘“)
- torch.nn.utils.clip_grad_norm
- keras.utils.vis_utils import plot_model 报错
- Gson 解析时候报错: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
- Gson() 解析json数据 列表类型
- flash.utils.Dictionary和Object
- Android应用中处理json数据:Gson 与 Jackson
- Utils--------工具类
- keras报错:ValueError: `steps_per_epoch=None` is only valid for a generator based on the `keras.utils.S
- Utils-IPUtil 处理ip聚、拆、起、终、区间计算等工具类
- Utils-Base64Util Base64String与byte[]互转
- Utils-Redis工具类