STRING TO JSON IN JAVA

JSON String example:

jsonString = {
  "productName" : "iPhone"
}

Converting JSON String to Java object:

import com.google.gson.Gson;
...
Gson g = new Gson();
JsonProduct jsonProduct = g.fromJson(jsonString, JsonProduct.class);

The Gson is an open-source library to deal with JSON in Java

<dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
</dependency>

Java Object:


import com.google.gson.annotations.SerializedName;

public class JsonProduct {

    @SerializedName( "productName")
    private String productName;
    

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    
}