Convert Java Collection / List / String[] Array Into JSON

In this example, we are going to convert a Java collection/list/string[] array into appropriate JSON String using GSON library

Note

I have already written an article about GSON, Convert Java Object To / From JSON, GSON Example, Please see it

Required Libraries

You need to download

  1. Google Gson

Following jar must be in classpath

  1. gson-2.2.4.jar
Project structure

Convert Java Object To / From JSON, GSON Example

Student.java

package net.javatips;

public class Student {

   
private String firstName = null;
   
private String lastName = null;

   
public Student(String firstName, String lastName) {
       
super();
       
this.firstName = firstName;
       
this.lastName = lastName;
   
}

   
public String getFirstName() {
       
return firstName;
   
}

   
public void setFirstName(String firstName) {
       
this.firstName = firstName;
   
}

   
public String getLastName() {
       
return lastName;
   
}

   
public void setLastName(String lastName) {
       
this.lastName = lastName;
   
}

   
@Override
   
public String toString() {
       
return "Student [firstName=" + firstName + ", lastName=" + lastName + "]";
   
}

}

Convert a Java collection/list/string[] array into JSON

Here we are creating List of student objects and passing this list into gson.toJson() method, which will accept Collectin,List, String[] etc.

package net.javatips;

import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class Main {

  
public static void main(String[] args) {
      
     
// Configure gson
     
GsonBuilder gsonBuilder = new GsonBuilder();
      Gson gson = gsonBuilder.create
();
     
     
//Convert List of string into JSON
     
List<String> strList = new LinkedList<String>();
      strList.add
("How");
      strList.add
("Are");
      strList.add
("You");
      System.out.println
("JSON String List "+gson.toJson(strList));
     

     
//Convert List of Object into JSON
     
List<Student> studentList = new LinkedList<Student>();
      Student student1 =
new Student("Sandra","Jose");
      Student student2 =
new Student("Sony","Jose");
      studentList.add
(student1);
      studentList.add
(student2);
      System.out.println
("JSON Object List "+gson.toJson(studentList));
     
     
//Convert Array of String into JSON
     
String[] strArray = new String[2];
      strArray
[0] = "Good";
      strArray
[1] = "Morning";
      System.out.println
("JSON String Array "+gson.toJson(strArray));
     
     
//Convert Array of Object into JSON
     
Student[] studentArray = new Student[2];
      studentArray
[0] = new Student("Rony","Jose");
      studentArray
[1= new Student("Aby","Jose");
      System.out.println
("JSON Object Array "+gson.toJson(studentArray));
    
  
}
}
Output
JSON String List ["How","Are","You"]
JSON Object List [{"firstName":"Sandra","lastName":"Jose"},{"firstName":"Sony","lastName":"Jose"}]
JSON String Array ["Good","Morning"]
JSON Object Array [{"firstName":"Rony","lastName":"Jose"},{"firstName":"Aby","lastName":"Jose"}]

 






Your email address will not be published. Required fields are marked *