Maven Dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
Student Pojo:
package com.ram.gson.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
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 List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.gson;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
Gson gson = new Gson();
String json = gson.toJson(student);
try {
FileWriter writer = new FileWriter("/home/ramakrishna/delete/json/student.json");
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(json);
}
}
Convert JSON To Java:
package com.ram.gson;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;
import com.ram.gson.pojo.Student;
public class JsonToJava {
public static void main(String[] args) {
Gson gson = new Gson();
try {
BufferedReader br = new BufferedReader(
new FileReader("/home/ramakrishna/delete/json/student.json"));
Student student = gson.fromJson(br, Student.class);
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
System.out.println("Values:"+student.getList());
} catch (IOException e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
Student Pojo:
package com.ram.gson.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
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 List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.gson;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
Gson gson = new Gson();
String json = gson.toJson(student);
try {
FileWriter writer = new FileWriter("/home/ramakrishna/delete/json/student.json");
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(json);
}
}
Convert JSON To Java:
package com.ram.gson;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;
import com.ram.gson.pojo.Student;
public class JsonToJava {
public static void main(String[] args) {
Gson gson = new Gson();
try {
BufferedReader br = new BufferedReader(
new FileReader("/home/ramakrishna/delete/json/student.json"));
Student student = gson.fromJson(br, Student.class);
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
System.out.println("Values:"+student.getList());
} catch (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment