Right now, you serialize your Pojo to String and then parse its String and convert it to a HashMap style object as a JSONObject .
It is very inefficient and brings nothing.
Jackson already provides an ObjectNode class to interact with your Pojo as a JSON object. So just convert your object to ObjectNode . Here is a working example
public class Example { public static void main(String[] args) throws Exception { Pojo pojo = new Pojo(); pojo.setAge(42); pojo.setName("Sotirios"); ObjectMapper mapper = new ObjectMapper(); ObjectNode node = mapper.valueToTree(pojo); System.out.println(node); } } class Pojo { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Otherwise, the way you do this is fine.
source share