Write Json Data -
public class Address {
String city;
int pin;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public Address() {
}
}
====================
public class Employee {
String name;
int age;
Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee() {
}
}
====================
public class WriteJson {
public static void main(String[] args) {
Address a=new Address();
a.setCity("Nagpur");
a.setPin(23);
Employee e=new Employee();
e.setName("raju");
e.setAge(12);
e.setAddress(a);
Address a1=new Address();
a1.setCity("Pune");
a1.setPin(32);
Employee e1=new Employee();
e1.setName("Sachin");
e1.setAge(12);
e1.setAddress(a1);
Gson g=new Gson();
System.out.println(g.toJson(e));
ArrayList<Employee> empList=new ArrayList<>();
empList.add(e);
empList.add(e1);
System.out.println(g.toJson(empList));
}
}
String city;
int pin;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public Address() {
}
}
====================
public class Employee {
String name;
int age;
Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee() {
}
}
====================
public class WriteJson {
public static void main(String[] args) {
Address a=new Address();
a.setCity("Nagpur");
a.setPin(23);
Employee e=new Employee();
e.setName("raju");
e.setAge(12);
e.setAddress(a);
Address a1=new Address();
a1.setCity("Pune");
a1.setPin(32);
Employee e1=new Employee();
e1.setName("Sachin");
e1.setAge(12);
e1.setAddress(a1);
Gson g=new Gson();
System.out.println(g.toJson(e));
ArrayList<Employee> empList=new ArrayList<>();
empList.add(e);
empList.add(e1);
System.out.println(g.toJson(empList));
}
}
Comments
Post a Comment