I am using jackson for converting POJO to JSON
User user = new User();
user.setAge(25);
user.setName("Shahid");
ObjectMapper mapper= new ObjectMapper();
mapper.writeValue("D:/test.json", user);
instead of writing it to file , I want to write it to on String variable (jsonString) . So that I get the result as follow.
String jsonString= "{"name" : "Shahid","age" : 25}";
You can try,
mapper.writeValueAsString(user).
Please refer documentation for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With