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());
}
}
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- eclipse maven m2e : Cannot complete the install - Eclipse
- M365 service Europe outage - AADSTS90033 A transient error has occurred. Please try again. - Microsoft
- How to install Node using Brew on Mac - MacOS
- Closest Alternate to Notepad on Mac - MacOS
- Java Code to check if Twitter app is installed on Android device - Android
- List of PowerShell Cmdlet Commands for Mac - Powershell
- Compare two lists in Python and return matches - Python
- How to turn off autocomplete in input fields in HTML Form - Html