Java JSONObject Example
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();
org.json
API in our application, we need to resolve following dependency.
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20220320</version> </dependency>
JSONObject
class.
Contents
1. Putting values to JSONObject
Theput
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);
{"101":"Mahesh","102":"Nilesh","103":"Jugesh"}
JSONObject jsonOb = new JSONObject(); jsonOb.put("2", 4); jsonOb.put("3", 9); jsonOb.put("4", 16); System.out.println(jsonOb);
{"2":4,"3":9,"4":16}
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);
{"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 fromJSONObject
, we can use following mehods.
Object get(String key)
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
TheJSONObject
makes JSON using following methods.
a.
String toString()
b.
String toString(int indentFactor)
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);
{ "101": "Mahesh", "102": "Nilesh" }
4. accumulate() Method
Theaccumulate()
method is declared as following.
JSONObject accumulate(String key, Object value)
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);
{"101":"Mahesh","102":["Nilesh","Jugesh"]}
5. append() Method
Theappend()
method is declared as following.
JSONObject append(String key, Object value)
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);
{"101":"Mahesh","102":["Nilesh"]} {"101":"Mahesh","102":["Nilesh","Jugesh"]}
6. getNames() Method
ThegetNames()
method is declared as following.
static String[] getNames(Object object)
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; }
name age
7. opt() Method
Theopt()
method is declared as following.
Object opt(String key)
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