Convert Java Object to JSON String | Code Factory
Download code and jars : Link.
File : EmployeeBean.java
package com.codeFactory.bean;
public class EmployeeBean {
private int id;
private String name;
private String gender;
private String designation;
// Getters and Setters...
}
File : Main.java
package com.codeFactory.main;
import java.util.HashMap;
import java.util.Map;
import com.codeFactory.bean.EmployeeBean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String args[]) {
EmployeeBean employeeBean = new EmployeeBean();
employeeBean.setId(1);
employeeBean.setName("employee1");
employeeBean.setGender("male");
employeeBean.setDesignation("JAVA");
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
map.put("1", "Welcome");
map.put("2", "to");
map.put("3", "Code Factory");
try {
String jsonStr = objectMapper.writeValueAsString(map);
System.out.println(jsonStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Tags :
Convert Java Object to JSON String
Convert Java Object to / from JSON
Converting Java objects to JSON with Jackson
How to convert Java object to JSON string?
how to convert Java object into JSON string using Jackson
To convert your object in JSON with Jackson
Convert POJO to Json
Comments
Post a Comment