Convert Java Object to / from JSON | 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 com.codeFactory.bean.EmployeeBean;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String args[]) {
EmployeeBean employeeBean = new EmployeeBean();
ObjectMapper objectMapper = new ObjectMapper();
// this create java object to json string...
String jsonStr = objectMapper.writeValueAsString(employeeBean);
// this create json string to java object...
EmployeeBean employeeBean2 = new EmployeeBean();
jsonStr = "{\"id\":\"2\",\"name\":\"Code Factory\"}";
employeeBean2 = objectMapper.readValue(jsonStr, EmployeeBean.class);
catch (Exception e)




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

Why we use Java at Jibit

[BDD] — Behaviour Driven Development with Java and Cucumber

Exception Handling in Java

How to Build Java Applications Today #63