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 16
863
Using org.json library:
try {
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
Log.d("Error", err.toString());
}
2 of 16
207
To anyone still looking for an answer:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
Dadroit
dadroit.com › string-to-json
Online String to JSON Converter
JSON is a lightweight format for data exchange, easily readable by both humans and machines. Primarily used in web APIs and server-web application communication, it is language-independent and a popular alternative to XML for data transmission and configuration files. To convert String to JSON, visit the tool address, input your String data —or load your String file— and the tool will display the corresponding JSON output in real time.
How to return a String as JSON from a @RestController
A String is not valid JSON, that’s correct. How should Spring know how to serialize that? More on reddit.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
How to convert a String into JSON String
http://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java More on teamtreehouse.com
How to convert json string to pojo if json response starts with [] instead of {} ??
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Videos
10:35
Parsing Json in Java Tutorial - Part 2: ObjectMapper and Generate ...
10:28
What is JSON - Convert Java Object To JSON using GSON - GSON tutorial ...
11:41
Convert Java Object to JSON String using Jackson Library - YouTube
14:24
Convert Java Object to JSON String | JSON String to Java Object ...
How to Convert a String to a javax.json.JsonObject in Java
Java ObjectMapper Tutorial: Convert Java Object to JSON & Back ...
JSONLint
jsonlint.com
JSONLint - The JSON Validator
JSONLint is the free online validator, json formatter, and json beautifier tool for JSON, a lightweight data-interchange format.
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 OverflowJSON
json.org
JSON
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
Reddit
reddit.com › r/javahelp › how to return a string as json from a @restcontroller
r/javahelp on Reddit: How to return a String as JSON from a @RestController
August 29, 2024 -
I'm using Spring Boot 3, I would like to return a String from an endpoint, not a Java object, I put the @RequestMapping annotation with produces = MediaType.APPLICATION_JSON_VALUE, the content type is set succesfully but the string is not escaped and the browser marks it as invalid json, it does not marshal it.
The RestController:
package com.example.api.controllers;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello World!!!";
}
}The idea is that Spring should marshal the string Hello World!!! to its JSON representation: "Hello World!!!" with double quotation marks
Top answer 1 of 4
3
A String is not valid JSON, that’s correct. How should Spring know how to serialize that?
2 of 4
2
use ResponseEntity. here you can use anything in responseEntity. You can pass String like example below. You can also Pass any DTO eg you have a DtoA. you can use ResponseEntity and so on. public ResponseEntity method(){ return new ResponseEntity<>("your string here",httpStatus); }
Mkyong
mkyong.com › home › java › convert java objects to json with jackson
Convert Java Objects to JSON with Jackson - Mkyong.com
April 24, 2024 - writeValue() – Convert Java object to JSON string.
Make Community
community.make.com › questions
How to convert strings that look like JSON to JSON objects - Questions - Make Community
November 17, 2024 - 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): I thought I might be able to simply string everything into one long path to keep it simple, but one of the routers is really necessary as the ‘trip’ can have no stops, one stop or five stops AND I can...
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 - To convert the above JSON string into an object user must-have class with the same property (same name). // creating object of Gson Gson gson = new Gson(); // calling method fromJson and passing JSON string into object // The first parameter is JSON string // The second parameter is the Java class to parse the JSON into an instance of.
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 ...
LabEx
labex.io › tutorials › java-how-to-convert-json-to-string-420796
How to convert JSON to string | LabEx
This tutorial provides comprehensive guidance on transforming JSON objects into string representations using various Java techniques and libraries, helping developers handle data serialization efficiently. JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write and simple for machines to parse and generate.
Jsonpathfinder
jsonpathfinder.com
JSON Path Finder
0: Enter your JSON in the editor. 1:Select an item to view its path.
JanBask Training
janbasktraining.com › community › java › how-can-i-convert-string-to-json-in-java-programming-language
How can I convert string to JSON in Java programming language? | JanBask Training Community
January 8, 2024 - Import com.google.gson.Gson; // Import Gson library Public class StringToJSON { Public static void main(String[] args) { String jsonString = “{”key1”: ”value1”, ”key2”: ”value2”}”; // Sample JSON string Gson gson = new Gson(); // Convert the string to a JSON object // Here, assume YourJSONObjectClass.class represents the structure of the JSON YourJSONObjectClass jsonObject = gson.fromJson(jsonString, YourJSONObjectClass.class); // Now you can work with the JSON object System.out.println(jsonObject.getKey1()); // Accessing a value by key } }
Top answer 1 of 2
1
org.json (included as standard in Android): https://github.com/douglascrockford/JSON-java
Jackson: https://github.com/FasterXML/jackson
2 of 2
0
Hi,
I know that I am very late responding to this question, but this is for future reference.
http://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java
I believe this a very easy way to do it. You can look at the JSONObject's documentation for further instruction.
http://www.json.org/javadoc/org/json/JSONObject.html
-Dan
Wikipedia
en.wikipedia.org › wiki › JSON
JSON - Wikipedia
March 6, 2005 - JSON (JavaScript Object Notation, pronounced /ˈdʒeɪsən/ or /ˈdʒeɪˌsɒn/) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of name–value pairs and arrays (or other serializable values).
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.*; 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 - If we want that kind of flexibility, we can switch to other libraries such as Jackson. A JSONArray is an ordered collection of values, resembling Java’s native Vector implementation: Values can be anything from a Number, String, Boolean, JSONArray, or JSONObject to even a JSONObject.NULL object