Java is a versatile programming language known for its robust performance and flexibility. When working with data in Java, you'll often encounter JSON (JavaScript Object Notation), a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. The java.util package provides the JSONObject class, which helps in working with JSON data.

JSON, as the name implies, is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. So, JSON is essentially composed of two structural types: a collection of name/value pairs, and a collection of values.

Working with JSONObject in Java
The JSONObject class in Java provides a simple way to create valid JSON objects and navigate or modify them. It also allows conversion between JSON and traditional Java types such as String, Map, List, Boolean, etc.

A JSONObject in Java is a(structure) of key-value pairs. Each key is a String and each value is either a basic value type or a JSONObject, which represents JSON object(i.e., collection of key-value pairs).
Creating a JSONObject

To create a simple JSON object, you can use one of the JSONObject() constructors. Here's an example:
JSONObject jsonObject = new JSONObject();
Adding Key-Value Pairs

After creating a JSONObject, you can add key-value pairs using the put() method:
jsonObject.put("key", "value");
Accessing Data in JSONObject

You can retrieve data from a JSONObject using several methods provided by the class, such as:
- get() - to get a value associated with a key
- getString() - to get a value as a string
- getInt() - to get a value as an integer
- getDouble() - to get a value as a double
- and so on...



















Accessing with get()
The get() method retrieves a value in whatever form it's stored:
Object value = jsonObject.get("key");
Accessing with getString(), getInt(), getDouble(), etc.
For more specific data types, you can use the specific get...() methods:
int intValue = jsonObject.getInt("key");
Android developers often use JSON in Java, for web services or when dealing with local JSON data files. It's an essential skill in today's modern programming environment. Understanding how to create and navigate JSONObjects is the first step in harnessing this power.