Using org.json library:

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}
Answer from dogbane on Stack Overflow
Top answer
1 of 11
714

To convert your object in JSON with Jackson:

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.ObjectWriter; 

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
2 of 11
57

I know this is old (and I am new to java), but I ran into the same problem. And the answers were not as clear to me as a newbie... so I thought I would add what I learned.

I used a third-party library to aid in the endeavor: org.codehaus.jackson All of the downloads for this can be found here.

For base JSON functionality, you need to add the following jars to your project's libraries: jackson-mapper-asl and jackson-core-asl

Choose the version your project needs. (Typically you can go with the latest stable build).

Once they are imported in to your project's libraries, add the following import lines to your code:

 import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.map.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;

With the java object defined and assigned values that you wish to convert to JSON and return as part of a RESTful web service

User u = new User();
u.firstName = "Sample";
u.lastName = "User";
u.email = "[email protected]";

ObjectMapper mapper = new ObjectMapper();
    
try {
    // convert user object to json string and return it 
    return mapper.writeValueAsString(u);
}
catch (JsonGenerationException | JsonMappingException  e) {
    // catch various errors
    e.printStackTrace();
}

The result should looks like this: {"firstName":"Sample","lastName":"User","email":"[email protected]"}

🌐
Tabnine
tabnine.com › home › convert java object to json
Convert Java object to JSON - Tabnine
July 25, 2024 - Converting a Java Obj to a JSON string is simple using JACKSON or GSON API. In our examples, we provided the code to make it easy for you to reproduce in your IDE. ... Create a new project (Maven is recommended).
🌐
Educative
educative.io › answers › how-to-convert-a-java-object-to-json
How to convert a Java object to JSON
The Google GSON​ library is the most popular library used for converting Java objects to JSON. The code to get a JSON String out of an object is given below.
Find elsewhere
🌐
Baeldung
baeldung.com › home › json › jackson › intro to the jackson objectmapper
Intro to the Jackson ObjectMapper | Baeldung
December 9, 2025 - This tutorial focuses on understanding the Jackson ObjectMapper class and how to serialize Java objects into JSON and deserialize JSON string into Java objects.
🌐
Baeldung
baeldung.com › home › json › convert string to jsonobject with gson
Convert String to JsonObject with Gson | Baeldung
May 5, 2025 - String json = "{ \"name\": ... .getAsBoolean()); In our second approach, we’ll see how to create a Gson instance and use the fromJson method....
🌐
Blogger
javarevisited.blogspot.com › 2022 › 03 › 3-examples-to-parse-json-in-java-using.html
3 ways to parse JSON String to Object in Java [Jackson, Gson, and json-simple Example]
April 22, 2023 - You just need to use the ObjectMapper class, which provides the readValue() method. This method can convert JSON String to Java object, that's why it takes JSON as a parameter and the Class instance in which you want to parse your JSON.
🌐
Medium
medium.com › @bectorhimanshu › parsing-a-string-into-a-jsonobject-in-java-and-vice-versa-1589b0b9edd2
Parsing a String into a JSONObject in Java and vice-versa | by bectorhimanshu | Medium
September 29, 2023 - In this blog, I will provide examples for both converting a JSONObject to a String and parsing a String into a JSONObject using the org.json.JSONObject class. a. To create a new JSONObject with desired properties in Java, we will need to use a JSON library. In this example, we’ll use the ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-json-string-to-java-object-using-gson
Convert Json String to Java Object Using GSON - GeeksforGeeks
July 11, 2025 - GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON. ... In this article, a predefined JSON String is converted into Java Object using GSON.
🌐
Java67
java67.com › 2016 › 10 › 3-ways-to-convert-string-to-json-object-in-java.html
3 ways to convert String to JSON object in Java? Examples | Java67
Jackson is a very popular and efficient Java library to map Java objects to JSON and vice-versa. If you want to learn the basics of the Jackson library and how to use them, I suggest you take a look at the Jackson documentation. That's all about how to convert String to JSON objects in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-java-object-to-json-string-using-gson
Convert Java Object to Json String using GSON - GeeksforGeeks
July 11, 2025 - GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON.
🌐
Medium
medium.com › @salvipriya97 › how-to-convert-java-pojo-to-json-using-jackson-2ef9b69c4f08
How to convert Java POJO to JSON using Jackson | by Priya Salvi | Medium
April 28, 2024 - We create an instance of the ... objects to JSON and vice versa. We use the writeValueAsString() method of the ObjectMapper instance to serialize the Person object to a JSON string....
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-java-object-to-json-using-jackson-library
How to convert Java object to JSON using Jackson library?
September 6, 2023 - Data Binding - converts JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations. It is of two type. Simple Data Binding - Converts JSON to and from Java Maps, Lists, Strings, Numbers, Booleans and null objects.
🌐
HKR Trainings
hkrtrainings.com › home › community › java › how can i convert string to json object in java?
How can I convert String to JSON object in Java? - Java - HKR Community
There are three rich open source libraries that allow you to convert String to JSON object efficiently and the name of the three libraries are given below: Gson Libraries(Comes From Google) JSON-Simple Libraries JACKSON Libraries · Before using any of the above libraries, you need to set the classpath of the relevant jar file. For Gson Libraries, you need to set the classpath of Gson.jar file and you can refer to this link http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm for downloading .
🌐
freeCodeCamp
freecodecamp.org › news › jsonobject-tostring-how-to-convert-json-to-a-string-in-java
JSONObject.toString() – How to Convert JSON to a String in Java
April 14, 2023 - In Java, it's common to work with JSON data, and an operation you'll frequently perform is converting a JSON object to a string representation.
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2018 › 09 › convert-java-object-to-json-string-and.html
JavaMadeSoEasy.com (JMSE): Convert java object to JSON string and pretty print Json in java - using Jackson
Convert JSON string to java Object - using Jackson · Convert java object to JSON string and pretty print Json in java - using Jackson · convert JSON string to java Map - using Jackson · convert java Map to JSON string · How to rename JSON properties in java- Jackson api ·