JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to convert Java object to JSON string?


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.

package com.java2novice.json.models;

public class Employee {

	private int empId = 1016;
	private String name = "Nataraja Gootooru";
	private String designation = "Programmer";
	private String department = "Java2Novice";
	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.java2novice.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":"Nataraja Gootooru","designation":"Programmer","department":"Java2Novice","salary":20000}
<< Previous Program | Next Program >>

Jackson JSON examples

  1. How to convert Java object to JSON string?
  2. How to convert JSON string to Java object?
  3. How to convert JSON string to Map using Jackson API?
  4. How to convert Map to JSON string using Jackson API?
  5. Enable JSON pretty print using Jackson API
  6. How to rename JSON properties using Jackson annotations?
  7. How to ignore JSON property using Jackson annotations?
  8. How to order JSON elements using Jackson annotations?
  9. How to ignore json empty or null values using Jackson API in java?
  10. How to handle date in Json using Jackson api in java?
  11. How to read specific json node in Jackson api (tree model)?
  12. Jackson API client - how to read json from URL?
Knowledge Centre
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.