Wednesday, June 27, 2012
Convert Java Objects to / from JSON using Gson
Here is the example code which will give you the overview of gson
package com.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class gsonObjectTry {
Gson gson = new Gson();
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("kalees", "24", "e3084"));
students.add(new Student("muthu", "25", "e3085"));
new gsonObjectTry().createStudents(students);
// new gsonObjectTry().addStudent(new Student("newStudent", "25", "e3085"));
// new gsonObjectTry().deleteStudent(Arrays.asList(new Student("muthu", "25", "e3085")));
new gsonObjectTry().updateStudent(Arrays.asList(new Student("muthu", "25", "e9999")));
// new gsonObjectTry().addNewStudent(new Student("new Student", "25", "e1111"));
}
public void createStudents(List<Student> students) {
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
Gson gson = new Gson();
String jobJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Students created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
// normally added newly
public void addStudent(Student student) {
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
Gson gson = new Gson();
String jobJson = gson.toJson(student);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Student created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
public void addNewStudent(Student student) {
try {
List<Student> students = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
students.add(student);
String jobJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Student created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
//build
public void deleteStudent(List<Student> deleteStudent) {
List<Student> students = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//all values
Iterator<Student> iterator = students.iterator();
//deleteable values
for (Student selectedInfo : deleteStudent) {
while (iterator.hasNext()) {
Student itrStudent = iterator.next();
System.out.println("itrStudent.getName() ======> " + itrStudent.getName());
System.out.println("selectedInfo.getName() ======> " + selectedInfo.getName());
if (itrStudent.getName().equals(selectedInfo.getName())) {
iterator.remove();
break;
}
}
}
} catch (Exception e) {
System.out.println("Student removing failed");
}
//write back to file
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
String studentsJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(studentsJson);
writer.flush();
System.out.println("Student deleted successfully");
} catch (IOException e) {
System.out.println("delete Student failed");
e.printStackTrace();
}
}
public void updateStudent(List<Student> updateStudent) {
List<Student> students = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//all values
Iterator<Student> iterator = students.iterator();
//deleteable values
for (Student selectedInfo : updateStudent) {
while (iterator.hasNext()) {
Student itrStudent = iterator.next();
System.out.println("itrStudent.getName() ======> " + itrStudent.getName());
System.out.println("selectedInfo.getName() ======> " + selectedInfo.getName());
if (itrStudent.getName().equals(selectedInfo.getName())) {
iterator.remove();
break;
}
}
}
students.addAll(updateStudent);
} catch (Exception e) {
System.out.println("Student updating failed");
}
//write back to file
// String studentsInfoJson = gson.toJson(students);
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
String studentsJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(studentsJson);
writer.flush();
System.out.println("Student updated successfully");
} catch (IOException e) {
System.out.println("delete updated failed");
e.printStackTrace();
}
}
}
class Student {
private String name;
private String age;
private String empNo;
public Student(String name, String age, String empNo) {
this.name = name;
this.age = age;
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
}
--
Thank you.
Regards,
Kaleeswaran.S
package com.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class gsonObjectTry {
Gson gson = new Gson();
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("kalees", "24", "e3084"));
students.add(new Student("muthu", "25", "e3085"));
new gsonObjectTry().createStudents(students);
// new gsonObjectTry().addStudent(new Student("newStudent", "25", "e3085"));
// new gsonObjectTry().deleteStudent(Arrays.asList(new Student("muthu", "25", "e3085")));
new gsonObjectTry().updateStudent(Arrays.asList(new Student("muthu", "25", "e9999")));
// new gsonObjectTry().addNewStudent(new Student("new Student", "25", "e1111"));
}
public void createStudents(List<Student> students) {
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
Gson gson = new Gson();
String jobJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Students created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
// normally added newly
public void addStudent(Student student) {
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
Gson gson = new Gson();
String jobJson = gson.toJson(student);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Student created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
public void addNewStudent(Student student) {
try {
List<Student> students = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
students.add(student);
String jobJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(jobJson);
writer.flush();
System.out.println("Student created successfully");
} catch (IOException e) {
System.out.println("createStudents failed");
e.printStackTrace();
}
}
//build
public void deleteStudent(List<Student> deleteStudent) {
List<Student> students = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//all values
Iterator<Student> iterator = students.iterator();
//deleteable values
for (Student selectedInfo : deleteStudent) {
while (iterator.hasNext()) {
Student itrStudent = iterator.next();
System.out.println("itrStudent.getName() ======> " + itrStudent.getName());
System.out.println("selectedInfo.getName() ======> " + selectedInfo.getName());
if (itrStudent.getName().equals(selectedInfo.getName())) {
iterator.remove();
break;
}
}
}
} catch (Exception e) {
System.out.println("Student removing failed");
}
//write back to file
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
String studentsJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(studentsJson);
writer.flush();
System.out.println("Student deleted successfully");
} catch (IOException e) {
System.out.println("delete Student failed");
e.printStackTrace();
}
}
public void updateStudent(List<Student> updateStudent) {
List<Student> students = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/kaleeswaran/Tried/gsonExample.json"));
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
students = gson.fromJson(bufferedReader, type);
bufferedReader.close();
//all values
Iterator<Student> iterator = students.iterator();
//deleteable values
for (Student selectedInfo : updateStudent) {
while (iterator.hasNext()) {
Student itrStudent = iterator.next();
System.out.println("itrStudent.getName() ======> " + itrStudent.getName());
System.out.println("selectedInfo.getName() ======> " + selectedInfo.getName());
if (itrStudent.getName().equals(selectedInfo.getName())) {
iterator.remove();
break;
}
}
}
students.addAll(updateStudent);
} catch (Exception e) {
System.out.println("Student updating failed");
}
//write back to file
// String studentsInfoJson = gson.toJson(students);
try {
//write converted json data to a file named "file.json"
File gsonFile = new File("/Users/kaleeswaran/Tried/gsonExample.json");
FileWriter writer = new FileWriter(gsonFile);
String studentsJson = gson.toJson(students);
writer = new FileWriter(gsonFile);
writer.write(studentsJson);
writer.flush();
System.out.println("Student updated successfully");
} catch (IOException e) {
System.out.println("delete updated failed");
e.printStackTrace();
}
}
}
class Student {
private String name;
private String age;
private String empNo;
public Student(String name, String age, String empNo) {
this.name = name;
this.age = age;
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
}
--
Thank you.
Regards,
Kaleeswaran.S
Subscribe to:
Posts (Atom)