当前位置:网站首页>Gson parsing and generating JSON data tool class
Gson parsing and generating JSON data tool class
2022-07-18 12:21:00 【five hundred and ninety-six million seven hundred and eighty-fi】
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Packaged GSON Parsing tool class , Provide generic parameters
*/
public class GsonUtil {
/**
* take Json The data is parsed into corresponding mapping objects
* @param clazz
* @param json
* @return
*/
public static Object getInstanceByJson(Class<?> clazz, String json)
{
Object obj = null;
Gson gson = new Gson();
obj = gson.fromJson(json, clazz);
return obj;
}
/**
* take Json The data is parsed into the corresponding set of mapping objects
* @param json
* @param clazz
* @return
*/
public static <T> List<T> jsonToList(String json, Class<T[]> clazz)
{
Gson gson = new Gson();
T[] array = gson.fromJson(json, clazz);
return Arrays.asList(array);
}
/**
* take Json The data is parsed into the corresponding set of mapping objects
* @param json
* @param clazz
* @return
*/
public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz)
{
Type type = new TypeToken<ArrayList<JsonObject>>()
{}.getType();
ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);
ArrayList<T> arrayList = new ArrayList<>();
for (JsonObject jsonObject : jsonObjects)
{
arrayList.add(new Gson().fromJson(jsonObject, clazz));
}
return arrayList;
}
/**
* Parse the object into Json data
* @param t
* @return json data
*/
public static <T> String ClassToJson(T t){
Gson gson = new Gson();
return gson.toJson(t);
}
}
边栏推荐
- HDU-单词数
- Interview frequency: how does MySQL ensure high availability?
- If the designer leaves, the iphone14 may become a brick, and using the iPhone is even more compelling
- openEuler 知:官方社区
- Excel-vba quick start (VII. Get cell objects)
- Map set to object, map field has horizontal lines, object to map
- GDB or delve debug Go program, check variable display < optimized out > solution
- [pyGame game] no admittance for disabled hands. The latest sadistic game "stitching" - a game you have to play.
- 洛谷题单-高精度
- JVM调优实战(详细版)
猜你喜欢
![Kettle [practice 02] TXT type files are classified and imported, execute SQL to convert data types and put them into storage (complete process instance cloud resource sharing: including sql+kjb+ktr+ t](/img/db/769ef66be162b79eae4b63598ca1cf.png)
Kettle [practice 02] TXT type files are classified and imported, execute SQL to convert data types and put them into storage (complete process instance cloud resource sharing: including sql+kjb+ktr+ t

Transformation of brushless motor on disk

Why do consumers buy iPhones instead of domestic ones? Because the depreciation of domestic mobile phones is too fast

Map set to object, map field has horizontal lines, object to map

中文版《开放敏捷架构精选示例案例研究》官网正式上线,精彩先睹为快!

【对象转换】vo2dto使用

Kettle【实践 01】Linux环境下使用Azkaban定时调用Kettle的KJB或KTR脚本实现自动化数据处理(完整流程实例分享:包含sql+ktr+shell+flow相关文件云资源)

【微信小程序】简洁好用的icon(94/100)

Use of gin framework

管网管理GIS服务平台有何功能?
随机推荐
Part 50 - Analysis of a query request header parameter [2022-07-14]
长安链介绍-01
Openeuler knowledge: management strategy
Kettle【实践 02】txt类型文件分类导入后执行SQL进行数据类型转换并入库(完整流程实例云资源分享:包含sql+kjb+ktr+测试文件)
Luogu questionnaire - high accuracy
Openeuler knowledge: official Community
Detailed explanation of assembly language programming skills (with examples)
My second anniversary of creation
使用nlmeas对图像进行去噪
C# 使用JObject解析嵌套json
Basic part of C language: pointer (elementary level)
The value of applet container technology in hybrid hybrid app development
What is the function of GIS service platform for pipe network management?
这20道Redis经典面试题你还不会,面试官看都不看你一眼
ThinkPHP code execution (cnvd-2018-24942)
Appium自动化测试基础 — webview操作(重点)
Redis data structure practice, see how microblogging, wechat, shopping cart, lottery applet is used?
openEuler 知:常用网址
三個步驟,一天就搞定了MySQL,讓我順利拿下了天猫offer
GDB common instructions