Home  >  org.json

Java JSONObject Example

By Arvind Rai, May 20, 2022
On this page we will learn using org.json.JSONObject class. The org.json API handles the JSON operation in Java application.
1. The JSONObject is an unordered collection of name/value pairs.
2. The JSONObject produces output as JSON string.
3. In JSONObject, we put values using put method that accepts key/value parameters. Key is string but value can be any datatype, for example, boolean, Collection, Map, int etc.
4. The toString() method makes a JSON text of this JSONObject.
5. Instantiate JSONObject as following.
JSONObject jsonOb = new JSONObject(); 
6. To use org.json API in our application, we need to resolve following dependency.
<dependency>
	<groupId>org.json</groupId>
	<artifactId>json</artifactId>
	<version>20220320</version>
</dependency> 
Now let us discuss methods of JSONObject class.



1. Putting values to JSONObject

The put method accepts key/value pair. Key is string and value can be any datatype for example boolean, Collection, double, float, int, long, Map and Object.
Find the methods to put values.
JSONObject put(String key, boolean value)
JSONObject put(String key, Collection<?> value)
JSONObject put(String key, double value)
JSONObject put(String key, Map<?,?> value) 

Find the examples.
1. Adding string value.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.put("102", "Nilesh");
jsonOb.put("103", "Jugesh");
System.out.println(jsonOb); 
Output
{"101":"Mahesh","102":"Nilesh","103":"Jugesh"} 
2. Adding integer value.
JSONObject jsonOb = new JSONObject();
jsonOb.put("2", 4);
jsonOb.put("3", 9);
jsonOb.put("4", 16);
System.out.println(jsonOb); 
Output
{"2":4,"3":9,"4":16} 
3. Adding Map value.
Map<Integer, String> map1 = new HashMap<>();
map1.put(101, "Mohit");
map1.put(102, "Suresh");
map1.put(103, "Anand");

Map<Integer, Integer> map2 = new HashMap<>();
map2.put(101, 25);
map2.put(102, 20);
map2.put(103, 30);      

JSONObject jsonOb = new JSONObject();
jsonOb.put("name", map1);
jsonOb.put("age", map2);

System.out.println(jsonOb); 
Output
{"name":{"103":"Anand","101":"Mohit","102":"Suresh"},"age":{"103":30,"101":25,"102":20}} 

2. Getting Values from JSONObject

To get the value by key from JSONObject, we can use following mehods.
Object get(String key) 
The JSONObject has more methods to get the values, for example,
getInt(String key) returns int value.
getBoolean(String key) returns boolean value.
getDouble(String key) returns double value.
getString(String key) returns String value.

Find the example.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.put("102", "Nilesh");
jsonOb.put("103", "Jugesh");

String name = jsonOb.getString("102"); // Nilesh
System.out.println(name); 



3. toString() Method

The JSONObject makes JSON using following methods.
a.
String toString() 
Make JSON with no whitespace.
b.
String toString(int indentFactor) 
Make a pretty-printed JSON text. The indentFactor is the number of spaces to add to each level of indentation.

Find the example.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.put("102", "Nilesh");

String jsonOutput = jsonOb.toString(2);
System.out.println(jsonOutput); 
Output
{
  "101": "Mahesh",
  "102": "Nilesh"
} 

4. accumulate() Method

The accumulate() method is declared as following.
JSONObject accumulate(String key, Object value) 
The accumulate() method accumulates values under a key. It is similar to put() method but the difference is that, for accumulate() method, when the key is already present, the values are stored as JSONArray.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.accumulate("102", "Nilesh");
jsonOb.accumulate("102", "Jugesh");
System.out.println(jsonOb); 
Output
{"101":"Mahesh","102":["Nilesh","Jugesh"]} 

5. append() Method

The append() method is declared as following.
JSONObject append(String key, Object value) 
The append method appends values to the array under a key.
If key is not available, then key is put in JSONObject with its value being JSONArray.
If key is available with value as JSONArray, then given value is appended to this array.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.append("102", "Nilesh");
System.out.println(jsonOb);
jsonOb.append("102", "Jugesh");
System.out.println(jsonOb); 
Output
{"101":"Mahesh","102":["Nilesh"]}
{"101":"Mahesh","102":["Nilesh","Jugesh"]} 

6. getNames() Method

The getNames() method is declared as following.
static String[] getNames(Object object) 
The getNames() method gets an array of public field names from an Object.
public class JSONDemo {
    public static void main(String... args) {
      String[] ob = JSONObject.getNames(new Student());
      for(String n: ob) {
    	System.out.println(n);
      }
    }
}
class Student {
  public int name;
  public int age;
} 
Output
name
age 

7. opt() Method

The opt() method is declared as following.
Object opt(String key) 
The opt method gets an optional value associated with a key. It returns an object which is the value, or null if there is no value.
JSONObject jsonOb = new JSONObject();
jsonOb.put("101", "Mahesh");
jsonOb.put("102", "Nilesh");

String data = (String)jsonOb.opt("102");
System.out.println(data); // Nilesh

data = (String)jsonOb.opt("103");
System.out.println(data); // null 

8. Reference

Class JSONObject
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us