Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an object to JSON representation

Tags:

java

json

Oddly enough, I didn't find this.

What's the simplest way to convert an object to a JSON string? (Edge cases like loops in the object graphs aren't of much interest to me. Let's find a solution to the simple case of class A that contains some objects of classes B,C,D and some primitives).

Basic collection support is a must.

like image 630
ripper234 Avatar asked Nov 10 '10 14:11

ripper234


1 Answers

Heh, I discovered/remembered what we are already using for this.

ObjectMapper from CodeHaus

The code looks like this - super simple:

Object obj = ...
String result = new ObjectMapper().writeValueAsString(obj);
like image 105
ripper234 Avatar answered Oct 26 '22 23:10

ripper234