![]() |
VOOZH | about |
JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data format used for data exchange. It is easy to read, write, and parse, making it widely used in web services and APIs. JSON files use the .json extension. JSON stores data in key–value pairs and supports arrays and nested objects.
Example: Storing student information in JSON format.
{
"Student": [
{
"Stu_id": "1001",
"Stu_Name": "Ashish",
"Course": "Java"
},
{
"Stu_id": "1002",
"Stu_Name": "Rana",
"Course": "Advance Java"
}
]
}
To read and write JSON data in Java, we commonly use third-party libraries. In this article, we use json-simple, a lightweight and beginner-friendly library.
Add the following dependency to your pom.xml file:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Class | Purpose |
|---|---|
JSONObject | Represents a JSON object (Map-based) |
JSONArray | Represents a JSON array (List-based) |
JSONValue | Parses JSON text |
JSONParser | Parses JSON from files or streams |
JSON encoding means converting Java data into JSON format.
Example: Create and Print JSON Object.
Output:
Explanation:
JSON decoding means converting JSON data into Java objects.
Output:
Explanation: