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, the JSONObject class provided by the org.json package is commonly used to create and manipulate JSON objects. The JSONObject.toString() method is a useful method provided by this class that converts a JSON object to a string representation.
Discussions

Converting Java objects to JSON with Jackson - Stack Overflow
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert strings that look like JSON to JSON objects
Greetings All, I am running into a problem where my JSON objects cease being JSON objects and become strings when I do set variable and get variable in order to have them in scope in the final (top) path where I am building my final JSON payload which causes formatting issues (See the red lines): ... More on community.make.com
๐ŸŒ community.make.com
0
0
November 17, 2024
Convert Java object to JSON without external library
Converting arbitrary objects to JSON isn't particularly easy, especially when there are full solutions already available. Personally, I use Gson, which is pretty easy to use. If the no external libraries is a HARD requirement, then your only choice would be to use reflection to get the names and values of the fields of the object to build the JSON string. You might have recursion problems if there are nested objects that create a circular loop, which complicates things. If you only have to serialize specific classes, then it would probably be easier to essentially hardcode the JSON string generation, and just call the appropriately getters to build the string. Basically, if it's only a few specific classes then you can write your own, if it's any arbitrary object then I highly recommend using a library, but if no libraries then I guess you could try homerolling your own JSON serializer with reflection. More on reddit.com
๐ŸŒ r/javahelp
12
1
May 1, 2020
Converting Json to Java object
Jackson is a library that people use to deserialize json to Java. More on reddit.com
๐ŸŒ r/javahelp
10
7
December 13, 2022
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ convert java objects to json with jackson
Convert Java Objects to JSON with Jackson - Mkyong.com
April 24, 2024 - readValue() โ€“ Convert JSON string to Java object. ObjectMapper mapper = new ObjectMapper(); // JSON file to Java object Person obj = mapper.readValue(new File("person.json"), Person.class); // JSON URL to Java object Person obj = mapper.readValue(new URL("http://some-domains/api/person.json"), Person.class); // JSON string to Java Object String json = "{\"name\": \"mkyong\", \"age\": 20}"; Person obj = mapper.readValue(json, Person.class); This example uses Jackson to convert a Java object Person to a JSON string.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ convert string to jsonobject with gson
Convert String to JsonObject with Gson | Baeldung
May 5, 2025 - String json = "{ \"name\": \"Baeldung\", \"java\": true }"; JsonObject jsonObject = JsonParser.parseString(json) .getAsJsonObject(); assertTrue(jsonObject.isJsonObject()); assertEquals("Baeldung", jsonObject.get("name") .getAsString()); assertTrue(jsonObject.get("java") .getAsBoolean()); In our second approach, weโ€™ll see how to create a Gson instance and use the fromJson method. This method deserializes the specified JSON String into an object of the specified class:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-convert-json-string-to-json-object
Java Program to Convert JSON String to JSON Object - GeeksforGeeks
January 30, 2022 - For more clarification follow the below example, to convert JSON String into JSON Object. ... // Java Program to demonstrate the // conversion of String to JSON object import com.google.gson.*; class GFG { int gfgId; String username; char gender; public GFG() { this.gfgId = 0; this.username = ""; this.gender = ' '; } } public class GFGMain { public static void main(String arg[]) { GFG gfg = null; // creating JSON String of GFG class object String jsonString; jsonString = "{"; jsonString += "gfgId : 10001,"; jsonString += "username : 'Jack jon',"; jsonString += "gender : 'M'"; jsonString += "}"; // creating object of gson Gson gson = new Gson(); // converting jsonStrig into object gfg = gson.fromJson(jsonString, GFG.class); System.out.println("GFG id of User : " + gfg.gfgId); System.out.println("Username : " + gfg.username); System.out.println("Gender : " + gfg.gender); } }
๐ŸŒ
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 - public class DemoApplication { ...t("IsOrderCompleted", true); // Convert the JSONObject to a JSON string String jsonString = newJson.toString(); // Print the JSON string System.out.println("JSON String: " + jsonString); ...
Find elsewhere
๐ŸŒ
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
The Gson is an open-source library to deal with JSON in Java programs. It comes from none other than Google, which is also behind Guava, a common purpose library for Java programmers. You can convert JSON String to Java object in just 2 lines by using Gson as shown below :
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-json-string-to-java-object-using-gson
Convert Json String to Java Object Using GSON - GeeksforGeeks
July 11, 2025 - Preprocess the JSON String and add slashes before passing it into GSON object. Example of Preprocessing: Initial JSON String: {"organisation_name" : "GeeksforGeeks", "description" : "A computer Science portal for Geeks", "Employee" : "2000"} ...
๐ŸŒ
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).
๐ŸŒ
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.
๐ŸŒ
GitHub
gist.github.com โ€บ madan712 โ€บ 4000053
Java program to Convert JSONObject to JSON String and vice versa ยท GitHub
December 19, 2015 - JSONObject part= new JSONObject(); part=new LinkedHashMap(); part.put("id",count); part.put("path","/latest/object/300000006637803/child/child_c); part.put("operation",operation); System.out.println(part.toJSONString());
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]"}

๐ŸŒ
Make Community
community.make.com โ€บ questions
How to convert strings that look like JSON to JSON objects - Questions - Make Community
November 17, 2024 - Your JSON string contains \n and \t, so it must be removed before giving it to ParseJSON. Use replace the function with a regular expression and remove extra \n and \t from your string.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ convert java object to json without external library
r/javahelp on Reddit: Convert Java object to JSON without external library
May 1, 2020 -

I feel like an idiot, I mean, I have been programming for a few years, mostly C# and Java, but all the REST apis I made was built with .NET, so I had no need for it.

Well, I can't find how to convert a Java object to a JSON WITHOUT the need of an external library, anyone?

๐ŸŒ
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.
๐ŸŒ
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?
July 3, 2020 - 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); } }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-java-object-to-json-string-using-jackson-api
Convert Java Object to Json String using Jackson API - GeeksforGeeks
May 10, 2022 - Convert the object into JSON using ObjectMapper class of Jackson API. ... // Java Program to Illustrate Object to JSON Conversion package com.Geeks; // Importing required classes import com.Geeks.Organisation; import java.io.IOException; import ...
๐ŸŒ
Studytonight
studytonight.com โ€บ java-examples โ€บ how-to-convert-string-to-json-and-vice-versa
How to Convert String to JSON and Vice Versa - Studytonight
JSON is JavaScript Object Notation and it is very popular for building web services like RESTful APIs. We used json, gson, and Jackson libraries to convert JSON to String and String to JSON.