

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a list of objects to JSON using the Gson library in Java?
A Gson is a library that can be used to convert Java Objects to JSON representation. It can also be used to convert a JSON string to an equivalent Java object. The primary class to use is Gson which we can create by calling the new Gson() and the GsonBuilder class can be used to create a Gson instance.
We can convert a list of objects by first creating a Person class and convert a list of Person objects to JSON.
Example
import java.util.*; import java.util.stream.*; import com.google.gson.*; public class JSONConverterTest { public static void main( String[] args ) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); List list = Stream.of(new Person("Raja", "Ramesh", 30, "9959984800"), new Person("Jai", "Dev", 25, "7702144400"), new Person("Adithya", "Sai", 21, "7013536200"), new Person("Chaitanya", "Sai", 28, "9656444150")) .collect(Collectors.toList()); System.out.println("Convert list of person objects to Json:"); String json = gson.toJson(list); // converts to json System.out.println(json); } } // Person class class Person { private String firstName, lastName, contact; private int age; public Person(String firstName, String lastName, int age, String contact) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.contact = contact; } public String toString() { return "[" + firstName + " " + lastName + " " + age + " " +contact +"]"; } }
Output
Convert list of person objects to Json: [ { "firstName": "Raja", "lastName": "Ramesh", "contact": "9959984800", "age": 30 }, { "firstName": "Jai", "lastName": "Dev", "contact": "7702144400", "age": 25 }, { "firstName": "Adithya", "lastName": "Sai", "contact": "7013536200", "age": 21 }, { "firstName": "Chaitanya", "lastName": "Sai", "contact": "9656444150", "age": 28 } ]
- Related Questions & Answers
- Convert a Map to JSON using the Gson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert Java object to JSON using GSON library?
- Convert JSON object to Java object using Gson library in Java?
- How to pretty print JSON using the Gson library in Java?
- How to write a JSON string to file using the Gson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- How to convert a JSON to Java Object using the Jackson library in Java?
- How to format a date using the Gson library in Java?
- How to convert HASHMAP to JSON using GSON in Android?
- Convert JSON to/from Map using Jackson library in Java?
- How to rename the properties of JSON using Gson in Java?
- How to convert Java object to JSON using Jackson library?
Advertisements