Convert JSON to List in Java
In this example program, we are showing simple program about, How to
Project Structure
1) Input JSON Data (data.json)
[{ "empId":101, "empName":"John", "empAge":21, "empQualification":"MS", "empSalary":10000 }, { "empId":102, "empName":"James", "empAge":22, "empQualification":"MBA", "empSalary":12000 }, { "empId":103, "empName":"William", "empAge":24, "empQualification":"MCA", "empSalary":13000 }, { "empId":104, "empName":"Smith", "empAge":23, "empQualification":"Msc", "empSalary":12500 }, { "empId":105, "empName":"Alex", "empAge":26, "empQualification":"MTech", "empSalary":15000 }]
2) Simple POJO Class (Employee.java)
package com.dineshkrish.json; public class Employee { private int empId; private String empName; private int empAge; private String empQualification; private double empSalary; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int getEmpAge() { return empAge; } public void setEmpAge(int empAge) { this.empAge = empAge; } public String getEmpQualification() { return empQualification; } public void setEmpQualification(String empQualification) { this.empQualification = empQualification; } public double getEmpSalary() { return empSalary; } public void setEmpSalary(double empSalary) { this.empSalary = empSalary; } }
3) Converting JSON to List Object
package com.dineshkrish.json; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.List; import com.google.gson.Gson; /** * * @author Dinesh Krishnan * */ public class ConvertJSONToList { public static void main(String[] args) { // Defining the list reference variable List employeeList = null; // defining the Gson object Gson gson = new Gson(); File jsonFile = new File("input/data.json"); try { FileReader reader = new FileReader(jsonFile); // convert JSON to List Object employeeList= (List)gson.fromJson(reader, List.class); System.out.println("JSON File Converted to List"); System.out.println("--------------------------"); // iterating the list object for(int i=0;i<employeeList.size();i++) { // converting the each element as 'Employee' object Employee employee = gson.fromJson(employeeList.get(i).toString(), Employee.class); System.out.println("ID : "+employee.getEmpId()); System.out.println("Name : "+employee.getEmpName()); System.out.println("Age : "+employee.getEmpAge()); System.out.println("Qualification : "+employee.getEmpQualification()); System.out.println("Salary : "+employee.getEmpSalary()); System.out.println(); } } catch (FileNotFoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Output
JSON File Converted to List
————————–
ID : 101
Name : John
Age : 21
Qualification : MS
Salary : 10000.0
ID : 102
Name : James
Age : 22
Qualification : MBA
Salary : 12000.0
ID : 103
Name : William
Age : 24
Qualification : MCA
Salary : 13000.0
ID : 104
Name : Smith
Age : 23
Qualification : Msc
Salary : 12500.0
ID : 105
Name : Alex
Age : 26
Qualification : MTech
Salary : 15000.0
References
1. Java List Interface
2. Java File Class
3. Java FileReader Class
4. Java GSON Class
5. Java fromJSON(..) Method
6. Convert JSON to Object in Java
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet