//POJO
package com.trail;
public class DataDTO {
String name;
String role;
String ID;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
}
Impl class..
package com.trail;
import com.google.gson.Gson;
public class TrailMain {
public static void main(String args[]){
DataDTO dataDTO=new DataDTO();
//set data in POJO
dataDTO.setID("1011");
dataDTO.setName("Sky");
dataDTO.setRole("Student");
//make it to the JSON String By useing GSON API
Gson gson=new Gson();
String jsonString=gson.toJson(dataDTO);
/*
* String jsonString contain the JSON data
* like-{"name":"Sky","role":"Student","ID":"1011"}
* */
System.out.println(jsonString);
//get data in POJO from JSON String
DataDTO dataDTO2=gson.fromJson(jsonString,DataDTO.class);
System.out.println(dataDTO2.getID());
System.out.println(dataDTO2.getName());
System.out.println(dataDTO2.getRole());
}
}
set Gson.jar in classpath and Compile these code..It will Show Out put in Console
It will ....
No comments:
Post a Comment