Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Java pojo to json String

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.

like image 409
Lilac Avatar asked Jan 09 '20 20:01

Lilac


People also ask

How to convert POJO to JSON and vice versa?

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.

How to convert a Java object to JSON?

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 ...

How to serialize Java object to JSON in Jackson?

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.

Is JSON easy to use in Java?

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 .


2 Answers

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);
like image 183
Vladucu Voican Avatar answered Sep 26 '22 14:09

Vladucu Voican


There are 2 libraries that deal with JSON serialization/deserialization using Java:

  1. Jackson

    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.

    Dependencies (databind is the main dependency, for annotations and additional features you will need more Jackson dependencies):

    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}"
    }
    

    Serialization Snippet:

    TabularDescriptor tabularDescriptor = new TabularDescriptor();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(tabularDescriptor);
    
  2. Gson

    Google's library for a Java serialization/deserialization(docs).

    Dependencies:

    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>
    

    Serialization Snippet:

    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:

  1. JSON in Java by Baeldung
  2. Jackson vs Gson by Baeldung
like image 21
improbable Avatar answered Sep 22 '22 14:09

improbable