I have the following java class
public class TabularDescriptor extends ReportDescriptor {
private String generatorClass;
private String targetClass;
private String name;
private String sublabel;
private String reportName;
private List<MappingMetadata> mappings = null;
private List<TabularColumnGroup> columnGroups = null;
private List<TabularStates> states = null;
:
:
and its getters and settere
I have entity classes for each of those List like MappingMetadata,TabularColumnGroup,TabularStates. I want to get a json data for this pojo classes. What can I do for it.
And what is the use of
public JSONObject toJSON() {
JSONObject ret = new JSONObject();
ret.put("generatorClass", this.generatorClass);
ret.put("targetClass", this.targetClass);
ret.put("name", this.name);
:
:
return ret;
}
And is there anyway I can display my json content on browser if yes how can I? Thanks.
Given a user defined object, we would like to convert Object ( or POJO) to JSON and vice versa. We will use the Google gson library to serialize POJO to JSON and deserialize JSON to POJO. Convert Person Object to JSON String.
These are the following steps to convert a Java object into JSON: 1 Create a Maven project. 2 Add GSON dependency in xml file. 3 Create Plain Old Java Object to convert into JSON. 4 Create a Java class to convert the Java object to JSON. More ...
The Jackson library is used to serialize the Java object to JSON and vice versa. The ObjectMapper class of the Jackson API provides methods to convert the Java object to JSON format or object. The ObjectMapper class writeValueAsString () method takes the JSON object as a parameter and returns its respective JSON string.
JSON format is simple to use. JSON is quite light-weight compared to other formats like XML etc,. JSON format can be easily converted into Java objects in an Object oriented manner. JSON is interoperable: program and platform independent. The most common way to convert Java Object to JSON string is to use an API .
I would recommend you to add Jackson to your project, it's rather easy to use.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
And in Java Code can be used as so:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(tabularDescriptor);
TabularDescriptor newTabularDescriptor = objectMapper.readValue(json, TabularDescriptor.class);
There are 2 libraries that deal with JSON serialization/deserialization using Java:
Another library for a Java serialization/deserialization(docs). A default choice for the JSON interaction within Java for the majority of developers. Comes completely embedded with all dependencies in spring-boot-starter-web
and spring-boot-starter-webflux
, dependency starters of Spring Boot - popular Java IOC/DI framework.
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
Gradle:
dependencies {
implementation "com.fasterxml.jackson.core:jackson-databind:${yourVersion}"
}
TabularDescriptor tabularDescriptor = new TabularDescriptor();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(tabularDescriptor);
Google's library for a Java serialization/deserialization(docs).
Gradle:
dependencies {
implementation "com.google.code.gson:gson:${yourVersion}"
}
Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
TabularDescriptor tabularDescriptor = new TabularDescriptor();
Gson gson = new Gson();
String json = gson.toJson(obj);
Details worth noting: you have to have all getters/setters public for both complete serialization and complete deserialization of an object(in its simplest form). An empty constructor is a must in any case.
Reference information:
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