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
๐ŸŒ
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.
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]"}

๐ŸŒ
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
It's very common nowadays to receive JSON String from a Java web service instead of XML, but unfortunately, JDK doesn't yet support conversion between JSON String to JSON object. Keeping JSON as String always is not a good option because you cannot operate on it easily, you need to convert it into a JSON object before you do anything else e.g.
๐ŸŒ
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).
Find elsewhere
๐ŸŒ
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....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-we-convert-a-json-string-to-a-json-object-in-java
How can we convert a JSON string to a JSON object in Java?
import org.json.JSONObject; import org.json.JSONArray; public class StringToJSONTest { public static void main(String args[]) { String str = "[{\"No\":\"1\",\"Name\":\"Adithya\"},{\"No\":\"2\",\"Name\":\"Jai\"}, {\"No\":\"3\",\"Name\":\"Raja\"}]"; JSONArray array = new JSONArray(str); for(int i=0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); System.out.println(object.getString("No")); System.out.println(object.getString("Name")); } } } ... import org.json.*; public class StringToJsonObjectTest { public static void main(String[] args) { String str = "{\"name\": \"Raja\", \"technology\": \"Java\"}"; JSONObject json = new JSONObject(str); System.out.println(json.toString()); String tech = json.getString("technology"); System.out.println(tech); } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - Although we have a way to serialize a Java object to JSON string, there is no way to convert it back using this library.
๐ŸŒ
OpsMatters
opsmatters.com โ€บ posts โ€บ 3-easiest-ways-convert-string-json-object-java
3 Easiest Ways to Convert String to JSON Object in Java? | OpsMatters
GSON is the first choice of many users because it can easily handle and manage complex graphs and nested structures. GSON has simplified the entire conversion process. With it, programmers can easily convert JSON strings to Java Objects.
๐ŸŒ
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.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java convert string to json object
How to Convert String to JSON Object in Java | Delft Stack
February 2, 2024 - We have different libraries available to perform this task. JSONObject can parse a string into a map-like object. It stores unordered key-value pairs. JSON-java library, commonly known as org.json, is used here with required maven dependency.
๐ŸŒ
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 ...
๐ŸŒ
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]
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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ intro to the jackson objectmapper
Intro to the Jackson ObjectMapper | Baeldung
December 9, 2025 - The methods writeValueAsString and writeValueAsBytes of ObjectMapper class generate a JSON from a Java object and return the generated JSON as a string or as a byte array:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-java-object-to-json-using-jackson-library
How to convert Java object to JSON using Jackson library?
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.
๐ŸŒ
Json2CSharp
json2csharp.com โ€บ code-converters โ€บ json-to-pojo
JSON to POJO Object Online Converter - Json2CSharp Toolkit
Here's how you can convert your JSON string to JAVA objects, we will be using the converter and external libraries like Jackson objectmapper to parse our object.
๐ŸŒ
Studytonight
studytonight.com โ€บ java-examples โ€บ how-to-convert-string-to-json-and-vice-versa
How to Convert String to JSON and Vice Versa - Studytonight
In the code given below, we have a JSON data stored in the form of String. Converting a string JSON is very much convenient to perform multiple actions. JSONObject is a class of org.json package that converts a string to an JSON object.