
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we convert a JSON string to a JSON object in Java?
The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data.
The JSONObject can parse text from a String to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The JSONArray can parse text from a String to produce a vector-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant array serialization.
In the below two examples, We can convert a JSON string to a JSON object.
Example 1
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")); } } }
Output
1 Adithya 2 Jai 3 Raja
Example 2
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); } }
Output
{"name":"Raja","technology":"Java"} Java
- Related Articles
- How can we convert a map to the JSON object in Java?
- How can we encode a JSON object in Java?
- How can we decode a JSON object in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- How can we parse a nested JSON object in Java?
- How can we convert a list to the JSON array in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert a JSON string into a JavaScript object?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert JSON text to JavaScript JSON object?
- How can we convert a JSON array to a list using Jackson in Java?\n
- How can we read a JSON file in Java?
- Convert a JSON object to XML format in Java?\n
- How to construct a JSON object from a subset of another JSON object in Java?

Advertisements