https://mkyong.com/java/gson-how-to-parse-json-arrays-an-array-of-arrays/java
In this article, we will show few JSON array examples and also how to use Gson
to map it back to Java Object.apache
<!-- Gson JSON -> Object --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
JSON basic.json
[ ]
= array{ }
= object1.1 Sample.ide
[ { "id": 1, "name": "a" }, { "id": 2, "name": "b" } ]
1.2 Gson
convert above JSON array into a List<Item>
.post
package com.mkyong; public class Item { private int id; private String name; }
package com.mkyong; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.util.List; public class JsonArray1 { public static void main(String[] args) throws IOException { JavaParseJsonArray main = new JavaParseJsonArray(); Gson gson = new Gson(); Type listType = new TypeToken<List<Item>>() {}.getType(); List<Item> list = gson.fromJson(main.loadFileFromClasspath("array1.json"), listType); System.out.println(gson.toJson(list)); } public String loadFileFromClasspath(String fileName) throws IOException { ClassLoader classLoader = getClass().getClassLoader(); try (InputStream inputStream = classLoader.getResourceAsStream(fileName)) { // common-io return IOUtils.toString(inputStream, StandardCharsets.UTF_8); } } }
P.S We use common-io
to convert inputStream
to String
.this
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
2.1 Sample.google
[ { "id": 1, "name": "a", "types": [ {"id": 1,"name": "a1"}, {"id": 2,"name": "a2"} ] }, { "id": 2, "name": "b", "types": [ {"id": 1,"name": "b1"}, {"id": 2,"name": "b2"} ] } ]
2.2 Gson
convert above JSON array into a List<Item>
containing List<ItemType> types
.idea
package com.mkyong; import java.util.List; public class Item { private int id; private String name; private List<ItemType> types; }
package com.mkyong; public class ItemType { private int id; private String name; }
2.3 Same code, no need to change.spa
Type listType = new TypeToken<List<Item>>() {}.getType(); List<Item> list = gson.fromJson(main.loadFileFromClasspath("array2.json"), listType);
3.1 Sample.debug
[ { "id": 1, "name": "a", "types": [ [ {"id": 1,"name": "a1"}, {"id": 2,"name": "a2"} ], [ {"id": 3,"name": "a3"} ] ] }, { "id": 2, "name": "b", "types": [ [ {"id": 1,"name": "b1"} ], [ {"id": 2,"name": "b2"} ] ] } ]
3.2 Change the types
to List<ItemType> types[];
package com.mkyong; import java.util.List; public class Item { private int id; private String name; private List<ItemType> types[]; // change types -> types[] }
package com.mkyong; public class ItemType { private int id; private String name; }
3.3 Same code, no need to change.
Type listType = new TypeToken<List<Item>>() {}.getType(); List<Item> list = gson.fromJson(main.loadFileFromClasspath("array3.json"), listType);
3.4 See the debugging mode.
4.1 This is a bit unusual JSON array sample.
[ { "id": 1, "name": "a", "types": [ [ "a1", 1 ], [ "a2", 2 ] ] }, { "id": 2, "name": "b", "types": [ [ "b1", 1 ] ] } ]
4.2 Change the types
to List<String> types[];
package com.mkyong; import java.util.List; public class Item { private int id; private String name; private List<String> types[]; }
4.3 Same code, no need to change.
Type listType = new TypeToken<List<Item>>() {}.getType(); List<Item> list = gson.fromJson(main.loadFileFromClasspath("array4.json"), listType);