to download eclipse https://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/neon3
Model or pojo
package test;
public class ProjectLIst {
String idProject;
String siteLocation;
String name;
String type;
String siteMangr;
public ProjectLIst(){}
public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
}
public String getIdProject() {
return idProject;
}
public void setIdProject(String idProject) {
this.idProject = idProject;
}
public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}
}
Model or pojo
package test;
public class ProjectLIst {
String idProject;
String siteLocation;
String name;
String type;
String siteMangr;
public ProjectLIst(){}
public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
}
public String getIdProject() {
return idProject;
}
public void setIdProject(String idProject) {
this.idProject = idProject;
}
public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}
}
##################################
package test;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
public class JsonCreating {
public static void main(String args[]){
ProjectLIst p1=new ProjectLIst("1","kochi","lee","steel","vimal");
ProjectLIst p2=new ProjectLIst("1","kochi","lfsdee","sdfdteel","errvimal");
ProjectLIst p3=new ProjectLIst("1","kochiee","wewelee","stedfel","erfdfvimal");
List<ProjectLIst> pList=new ArrayList<ProjectLIst>();
pList.add(p1);
pList.add(p2);
pList.add(p3);
Gson gson = new Gson();
Type type = new TypeToken<List<ProjectLIst>>() {}.getType();
//String json = gson.toJson(list, type);
String myjson=gson.toJson(pList,type);
System.out.println(myjson);
}
}
############################
Nested
package test;
import java.util.List;
public class ProjectLIst {
String idProject;
String siteLocation;
String name;
String type;
String siteMangr;
List<Phone> phons;
public ProjectLIst(){}
public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr,List<Phone> ph){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
this.phons=ph;
}
public List<Phone> getPhons() {
return phons;
}
public void setPhons(List<Phone> phons) {
this.phons = phons;
}
public String getIdProject() {
return idProject;
}
public void setIdProject(String idProject) {
this.idProject = idProject;
}
public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}
}
############################
Nested
package test;
import java.util.List;
public class ProjectLIst {
String idProject;
String siteLocation;
String name;
String type;
String siteMangr;
List<Phone> phons;
public ProjectLIst(){}
public ProjectLIst(String idProject,String siteLocation,String name,String type,String siteMangr,List<Phone> ph){
this.idProject=idProject;
this.siteLocation=siteLocation;
this.name=name;
this.type=type;
this.siteMangr=siteMangr;
this.phons=ph;
}
public List<Phone> getPhons() {
return phons;
}
public void setPhons(List<Phone> phons) {
this.phons = phons;
}
public String getIdProject() {
return idProject;
}
public void setIdProject(String idProject) {
this.idProject = idProject;
}
public String getSiteLocation() {
return siteLocation;
}
public void setSiteLocation(String siteLocation) {
this.siteLocation = siteLocation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSiteMangr() {
return siteMangr;
}
public void setSiteMangr(String siteMangr) {
this.siteMangr = siteMangr;
}
}
----
package test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class JsonNested {
public static void main(String args[]){
Phone ph1=new Phone();
ph1.setPhnnum("33333333");
Phone ph=new Phone();
ph.setPhnnum("33333333");
List<Phone> phonelist=new ArrayList<Phone>();
phonelist.add(ph);
phonelist.add(ph1);
ProjectLIst p1=new ProjectLIst("1","kochi","lee","steel","vimal",phonelist);
ProjectLIst p2=new ProjectLIst("1","kochi","lfsdee","sdfdteel","errvimal",phonelist);
ProjectLIst p3=new ProjectLIst("1","kochiee","wewelee","stedfel","erfdfvimal",phonelist);
List<ProjectLIst> pList=new ArrayList<ProjectLIst>();
pList.add(p1);
pList.add(p2);
pList.add(p3);
Gson gson = new Gson();
Type type = new TypeToken<List<ProjectLIst>>() {}.getType();
//String json = gson.toJson(list, type);
String myjson=gson.toJson(pList,type);
System.out.println(myjson);
}
}
output
[{"idProject":"1","siteLocation":"kochi","name":"lee","type":"steel","siteMangr":"vimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]},{"idProject":"1","siteLocation":"kochi","name":"lfsdee","type":"sdfdteel","siteMangr":"errvimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]},{"idProject":"1","siteLocation":"kochiee","name":"wewelee","type":"stedfel","siteMangr":"erfdfvimal","phons":[{"phnnum":"33333333"},{"phnnum":"33333333"}]}]
No comments:
Post a Comment