If you have a GSON String which is an object collection and you want to convert it into an ArrayLis<> using the Gson library, make use of the reflection TypeToken
ArrayList<Student> stdList = new ArrayList<>();
stdList = gson.fromJson(jsonList, new TypeToken<ArrayList<Student>>(){}.getType());
System.out.println(stdList(0).getStudentName());
System.out.println(stdList(1).getStudentName());
Example:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
public class JsonToGsonExample {
public static void main(String[] args) {
String jsonString = "{\n" +
" \"studentId\": 1,\n" +
" \"studentName\": \"Mike\",\n" +
" \"studentAddress\": \"Paris\",\n" +
" \"studentAge\": 19,\n" +
" \"studentMarks\": {\n" +
" \"English\": 87,\n" +
" \"Chemistry\": 72,\n" +
" \"Math\": 75,\n" +
" \"Physics\": 67\n" +
" }\n" +
"}";
String jsonStringList ="[\n" +
" {\n" +
" \"studentId\": 1,\n" +
" \"studentName\": \"Mike\",\n" +
" \"studentAddress\": \"Paris\",\n" +
" \"studentAge\": 19,\n" +
" \"studentMarks\": {\n" +
" \"English\": 87,\n" +
" \"Chemistry\": 72,\n" +
" \"Math\": 75,\n" +
" \"Physics\": 67\n" +
" }\n" +
" },\n" +
" {\n" +
" \"studentId\": 2,\n" +
" \"studentName\": \"Alex\",\n" +
" \"studentAddress\": \"London\",\n" +
" \"studentAge\": 18,\n" +
" \"studentMarks\": {\n" +
" \"English\": 71,\n" +
" \"Chemistry\": 98,\n" +
" \"Math\": 89,\n" +
" \"Physics\": 76\n" +
" }\n" +
" }\n" +
"]";
Gson gson = new Gson();
//Example with just one Json Object
Student student = gson.fromJson(jsonString,Student.class);
System.out.println(student.getStudentName());
System.out.println(student.getStudentAddress());
//Example with a Student List
ArrayList<Student> studentsList = new ArrayList<>();
studentsList = gson.fromJson(jsonStringList, new TypeToken<ArrayList<Student>>(){}.getType());
System.out.println(studentsList.get(0).getStudentName());
System.out.println(studentsList.get(1).getStudentName());
}
}
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- 9 Border to DIV Element in HTML Examples - Html
- How to turn off Dark Mode in Excel App for Mac - Windows
- The Android Virtual Device is currently running in an emulator and cannot be deleted - Android
- Show Notepad++ tabs in multiple lines instead of scroll on tab bar - NotepadPlusPlus
- How to know if you have blocked your friend Number on Android Phone - Android
- Run SQL Script file using Java JDBC Code Example - Java
- How to show console in Eclipse IDE - Eclipse
- Install GCC on M1/M2 ARM Based Mac - MacOS