object to json string java code example

Example 1: how to convert jsonobject to string in java

JSONObject json = new JSONObject();

json.toString();

Example 2: convert json string to json object in java

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}

Example 3: how to convert object in api

DE-SERIALIZATION:  JSON --> JAVA OBJECT
SERIALIZATION: 	   JSON <-- JAVA OBJECT

I add jacksonDataBinder dependency in my pom.xml file -> 
it is a json parser. That is used for converting
from java object to json and from json to java object.

Example 4: convert java object to json

Jackson Data-bind depdendency that take care of 
Converting between 
JSON to Java Object 
and 
Java Object to JSON

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.0</version>
</dependency>

Tags:

Java Example