Convert Java Object to JSON String | Code Factory

package com.codeFactory.bean;public class EmployeeBean {private int id;
private String name;
private String gender;
private String designation;
// Getters and Setters...

}
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();
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Types of variables in Java

Garbage Collector(GC) in Java

Java Packages & API-10 example

Null check strategy in HTTP PATCH with Java 8 Consumer