How to convert an object to a JSON representation

Oddly enough, I did not find this.

What is the easiest way to convert an object to a JSON string? (Boundary cases, such as cycles in graphs of objects, are not of great interest to me. We will find a solution to the simple case of class A containing some objects of classes B, C, D and some primitives).

Support for the base collection is required.

+3
source share
4 answers

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

ObjectMapper from CodeHaus

The code is as follows: super simple:

Object obj = ...
String result = new ObjectMapper().writeValueAsString(obj);
+3
source

Gson Google . .

+2

I would recommend JAXB + Jackson. See question for details .

+2
source

See the answer here: Javascript for Java using JSON

The answer applies in both directions, they are bi-directional.

+2
source

Source: https://habr.com/ru/post/1774106/


All Articles