Skip to content

Instantly share code, notes, and snippets.

@faizakram
Last active August 8, 2018 12:12
Show Gist options
  • Save faizakram/6a5dd8f735c9ccf231e008204ae61e64 to your computer and use it in GitHub Desktop.
Save faizakram/6a5dd8f735c9ccf231e008204ae61e64 to your computer and use it in GitHub Desktop.
Covert Java Object in json
How to write to raw string with jackson 2?
String result ="";
try {
result = objectMapper.writeValueAsString(objectMapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
//Output like this "[{\"id\":1,\"name\":\"Dinesh\",\"age\":20},{\"id\":2,\"name\":\"Kamal\",\"age\":15},{\"id\":3,\"name\":\"Vijay\",\"age\":25},{\"id\":4,\"name\":\"Dinesh\",\"age\":30},{\"id\":5,\"name\":\"Dinesh\",\"age\":18}]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment