发布于 1年前

Java转换List为JSON格式

使用GSON可以很简单实现list转换为json格式。

添加依赖

<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
</dependencies>

可以在http://mvnrepository.com/artifact/com.google.code.gson/gson查看最新的版本。

用法

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
String json = new Gson().toJson(list);

JSON转换为list

Type type = new TypeToken<List<Student>>() {}.getType();
List<Student> students = gson.fromJson(jsonStudents, type);
©2020 edoou.com   京ICP备16001874号-3