当前位置:网站首页>Gson解析生成json数据工具类
Gson解析生成json数据工具类
2022-07-16 01:25:00 【596785154】
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;
/**
* 封装的GSON解析工具类,提供泛型参数
*/
public class GsonUtil {
/**
* 将Json数据解析成相应的映射对象
* @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;
}
/**
* 将Json数据解析成相应的映射对象集合
* @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);
}
/**
* 将Json数据解析成相应的映射对象集合
* @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;
}
/**
* 将对象解析成Json数据
* @param t
* @return json数据
*/
public static <T> String ClassToJson(T t){
Gson gson = new Gson();
return gson.toJson(t);
}
}
边栏推荐
- 在conda中添加清华镜像
- Pule frog 4d5d dynamic cinema | VR space travel equipment | VR takes you to travel in space
- openEuler 知:ip addr 查不到 ip 的解决方法
- GDB common instructions
- Binary tree traversal
- . Miniapi of net7 (special article):preview6 caching and stream limiting
- 第50篇-某查查请求头参数分析【2022-07-14】
- Image denoising using nlmeas
- 包含数字的字符串剔除字母根据步长递增
- Openeuler knowledge: sig
猜你喜欢

What are the five common data types of redis? What is the corresponding data storage space? Take you to learn from scratch

thinkphp 代码执行 (CNVD-2018-24942)
![[vscode] shortcut key to switch between file and editor](/img/33/3975a3ba04caf5ff6de5d2dd90fb4d.png)
[vscode] shortcut key to switch between file and editor

openEuler 知:ip addr 查不到 ip 的解决方法

JupyterLab安装

Heavyweight: Mobileye officially announced the postponement of IPO, slowing down the growth of revenue and intensifying market competition

Jupyterab installation

Using mysql+excel to process data

社区峰会|Pulsar Summit 旧金山峰会议题亮点曝光!

Overview of database system -- overview of data model
随机推荐
UE5简单的角色碰撞检测功能
深度学习------验证码
2022年成都/杭州/厦门/武汉产品经理认证招生简章(NPDP)
Ftxui basic notes (Hello World)
Analysis of common interview questions in MySQL
Openeuler knows: the solution of IP addr not finding IP
When Scala reads the input content in the command-line environment, the input content does not display the problem (unresolved)
go自动化执行命令记录
Résolution du format d'image
Ue5 simple role collision detection function
Deep learning ----- different methods to realize resnet-18 and resnet34
. Miniapi of net7 (special article):preview6 caching and stream limiting
Gao fushuai in unit testing, pytest framework (end) test report
[LSTM regression prediction] Based on MATLAB tpa-lstm time attention mechanism, long-term and short-term memory neural network regression prediction (multiple input and single output) [including Matla
【Jailhouse 文章】Bao: A Lightweight Static Partitioning Hypervisor for Modern Multi-Core Embedded...
二叉树遍历
openEuler 知:日志查找技巧
数据库定时备份linux篇
TMUX usage
长安链介绍-01