Convert POJO tO Json String
Download Jar: https://www.box.com/s/vm59rumsblk4p25kim8f
Create Pojo class in your project :
Create Main method as show below:
When you execute this project output is:
Output: {"age":666,"name":"Person Name","class":"class Person"}
Download Jar: https://www.box.com/s/vm59rumsblk4p25kim8f
Create Pojo class in your project :
/**
* @author t4ecke4
*/
public class Person {
private String name;
private Integer age;
public String getName() { return this.name; }
public void setName( String name ) { this.name = name; }
public Integer getAge() { return this.age; }
public void setAge( Integer age ) { this.age = age; }
}
* @author t4ecke4
*/
public class Person {
private String name;
private Integer age;
public String getName() { return this.name; }
public void setName( String name ) { this.name = name; }
public Integer getAge() { return this.age; }
public void setAge( Integer age ) { this.age = age; }
}
Create Main method as show below:
import org.json.JSONObject;
public class PojoToJson {
/**
* @author t4ecke4
*/
public static void main(String[] args) {
Person person = new Person();
person.setName( "Person Name" );
person.setAge( 666 );
JSONObject jsonObj = new JSONObject( person );
System.out.println( jsonObj );
}
}
public class PojoToJson {
/**
* @author t4ecke4
*/
public static void main(String[] args) {
Person person = new Person();
person.setName( "Person Name" );
person.setAge( 666 );
JSONObject jsonObj = new JSONObject( person );
System.out.println( jsonObj );
}
}
When you execute this project output is:
Output: {"age":666,"name":"Person Name","class":"class Person"}
Hi,
ReplyDeletecan you please tell me how to convert the multi level pojo to json.My use case is like this.. There is one pojo name A and inside this pojo we have another POJO say B.now I want to create the JSON for both pojo then how to do that..
if it is for multiple values then how it is done
ReplyDelete