0% found this document useful (0 votes)
77 views

Convert JavaObject To JSON

This document provides steps to convert a Java object to a JSON string using Jackson's data binding library. It includes adding the Jackson dependency to a Maven project, defining an Employee POJO class with sample data, and using the ObjectMapper to write the Employee object as a JSON string. The output JSON string contains the field names and values of the Employee object.

Uploaded by

Radheshyam Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Convert JavaObject To JSON

This document provides steps to convert a Java object to a JSON string using Jackson's data binding library. It includes adding the Jackson dependency to a Maven project, defining an Employee POJO class with sample data, and using the ObjectMapper to write the Employee object as a JSON string. The output JSON string contains the field names and values of the Employee object.

Uploaded by

Radheshyam Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Hi Folks,

#Today's uploaded #Real_Time


Q.#How to #convert #Java object to #JSON string?
Ans-This page shows how to convert java object to JSON string using Jackson's data
binding.
As a first step add Jackson dependent jar file "jackson-mapper-asl" to your
classpath. Here is the pom.xml file for your reference:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jackson_exmp</groupId>
<artifactId>jackson_exmp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
</dependencies>
</project>
Create a simple Employee pojo. We will convert this pojo to JSON value. Note that
we have already initialized the Employee class with default values.

3
package com.javacoffee.json.models;

public class Employee {

private int empId = 1016;


private String name = "pratik kumar";
private String designation = "Programmer";
private String department = "Javacoffee";
private int salary = 20000;

public String toString(){


StringBuilder sb = new StringBuilder();
sb.append("************************************");
sb.append("\nempId: ").append(empId);
sb.append("\nname: ").append(name);
sb.append("\ndesignation: ").append(designation);
sb.append("\ndepartment: ").append(department);
sb.append("\nsalary: ").append(salary);
sb.append("\n************************************");
return sb.toString();
}

public int getEmpId() {


return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Finally here is the example to convert java object to JSON string:

package com.javacoffee.json.examples;

import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;

import com.java2novice.json.models.Employee;

public class ObjectToJsonEx {

public static void main(String[] a){

Employee emp = new Employee();


ObjectMapper mapperObj = new ObjectMapper();

try {
// get Employee object as a json string
String jsonStr = mapperObj.writeValueAsString(emp);
System.out.println(jsonStr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Output:
{"empId":1016,"name":"pratik
kumar","designation":"Programmer","department":"Javacoffee","salary":20000}

You might also like