JsonObject Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

We most frequently utilize JSON (JavaScript Object Notation), a lightweight data-exchange format, for client-server communication. It is both simple to read and write and language-independent.  JSON object, an array, an integer, a text, a boolean (true/false), or null can all be used as JSON values.

In this tutorial, we'll learn how to produce, edit, and parse JSON using the JSONObject in java, also known as org.json, one of the JSON processing libraries that is readily available.

Introduction to Java JsonObject

We can read and write JSON data in Java using the JSONObject in java(json.simple package). In other words, the json package allows us to encode and decode JSON objects in Java. Important classes for the JSON API are contained in the org.json.simple package.

  • JSONValue
  • JSONObject
  • JSONArray
  • JsonString
  • JsonNumber

Declaration

Mapping between JSON and Java Entities

While encoding or parsing JSON.simple, entities are mapped from the right to the left, and when decoding or parsing, they are mapped from the left to the right.

JSONJAVA
stringjava.lang.String
numberjava.lang.Number
true,falsejava.lang.Boolean
nullnull
arrayjava.util.list
objectjava.lang.Map

Constructors Summary of Java JsonObject

ConstructorDescription
JSONObject()a JSONObject in java is created without name/value mappings.
JSONObject(Map mp)copies every name/value mapping from the supplied map and creates a new JSONObject.
JSONObject(JSONTokener readfrom)creates a new JSONObject from the following object in the tokener with name/value mappings.
JSONObject(String s)creates a new JSONObject from the JSON string with name/value mappings.
JSONObject(JSONObject copyFrom, String[] name)copies the mappings for the stated names from the supplied object and creates a new JSONObject.

Nested Class Summary of Java JsonObject

JsonObject in java is inherited from different interfaces and classes. Here is list of classes/interfaces that are parent classes of jsonObject.

modifier and typeinterface and description
static classJsonValue.ValueType displays a JsonValue object's type.
static interfaceA map entry (key-value pair)

Field Summary of Java JsonObject

A Field gives users dynamic access to and details about a specific field of a class or interface. A class (static) field or an instance field could be the mirrored field.

Fielddescription
static final JsonValue NULLJSON null value
static final JsonValue FALSEJSON False value
static final JsonValue TRUEJSON True value

Method Summary of Java JsonObject

MethodDescription
boolean getBoolean(String name)returns the associated mapping's boolean value for the given name.
boolean getBoolean(String name, boolean defaultValue)The corresponding mapping's boolean value for the given name is returned.int getInt(String name)A convenience method for getJsonNumber(name).intValue()
int getInt(String name, int defaultValue)gives the supplied name's corresponding int value for the JsonNumber mapping.
JsonArray getJsonArray(String name)gives the array value that the given name is mapped to.
JsonNumber getJsonNumber(String name)returns the numerical value that corresponds to the provided name.
JsonObject getJsonObject(String name)returns the value of the object that corresponds to the given name.
JsonString getJsonString(String name)returns the string value that corresponds to the given name.
String getString(String name)an effective getJsonString method (name). getString()
String getString(String name, String defaultValue)Returns the corresponding JsonString mapping's string value for the given name.
boolean isNull(String name)If JsonValue.NULL is the corresponding value for the given name, then the function returns true.

Method Detail

getJsonArray

Syntax

The array value to which the supplied name is mapped is returned. This is a shortcut for (JsonArray)get(name) to get the value.

Parameters:

  • name - the name associated with the value to be returned

Returns: the array value to which the given name is mapped, or null if there is no mapping for the name in this object.

Throws:

  • ClassCastException - if the value mapped to the given name is not assignable to the JsonArray type

getJsonObject

Syntax

The object value to which the given name is mapped is returned. This is a shortcut for (JsonObject)get(name) to return the value. Parameters:

  • name - the name associated with the value to be returned Returns: the object value to which the supplied name is mapped, or null if there is no mapping for the name in this object. Throws:
  • ClassCastException - if the value mapped to the supplied name is not assignable to the JsonObject type

getJsonNumber

Syntax

The numeric value to which the provided name is mapped is returned. This is a quick way to acquire the value of (JsonNumber)get(name).

Parameters:

  • name - the name associated with the value to be returned

Returns: the numeric value to which the supplied name is mapped, or null if there is no mapping for the name in this object.

Throws:

  • ClassCastException - if the value mapped to the supplied name is not assignable to the JsonNumber type

getJsonString

Syntax

Returns the string value that corresponds to the provided name This is a shortcut for (JsonString)get(name) to return the value. Parameters:

  • name - the name associated with the value to be returned Returns: the string value to which the supplied name is mapped, or null if there is no mapping for the name in this object. Throws:
  • ClassCastException - if the value mapped to the provided name is not assignable to the JsonString type

getString

Syntax

A shortcut for getJsonString (name). getString() Parameters:

  • name - whose value is to be returned as a String Returns: the String value that corresponds to the provided name Throws:
  • NullPointerException - if there is no mapping for the supplied name
  • ClassCastException - if the supplied name mapping's value is not assignable to JsonString

getString

String getString(String name, String defaultValue) The string value of the corresponding JsonString mapping for the supplied name is returned. If a JsonString is discovered, its JsonString.getString() method is called. Otherwise, the default value supplied is returned. Parameters:

  • name - the name of the variable whose associated value is to be provided as a String.
  • defaultValue - the default value to be returned Returns: the string value of the associated mapping for the name, or the default value

getInt

Syntax

A shortcut for getJsonNumber (name). intValue() Parameters: name - whose value is to be returned as an int Returns: the int value that corresponds to the provided name Throws:

  • NullPointerException - if no mapping exists for the provided name.
  • ClassCastException - thrown if the value for the provided name mapping cannot be assigned to JsonNumber.

getInt

Syntax

The int value of the related JsonNumber mapping for the supplied name is returned. If JsonNumber is discovered, JsonNumber.intValue() is returned. Otherwise, the default value supplied is returned. Parameters:

  • name - the name of the variable whose associated value is to be returned as an int
  • defaultValue - the default value to be provided Returns: the int value of the name's associated mapping, or the default value

getBoolean

Syntax

Returns the boolean value of the related mapping for the supplied name. If the associated mapping is JsonValue. TRUE, then returns TRUE. If the associated mapping is JsonValue. FALSE, then returns false. Parameters:

  • name - whose associated boolean value is to be returned

Returns: the boolean value to which the supplied name is mapped Throws:

  • NullPointerException - if no mapping exists for the specified name
  • ClassCastException - If the value for the supplied name mapping is not assignable to JsonValue. TRUE or JsonValue FALSE

getBoolean

Syntax

The boolean value of the related mapping for the supplied name is returned. Returns true if the related mapping is JsonValue.TRUE. If the corresponding mapping is JsonValue. FALSE Then it returns false. Otherwise, the default value supplied is returned. Parameters:

  • name - the name of the variable whose associated value is to be returned as an int
  • defaultValue - the default value to be returned

Returns: The related mapping's boolean value for the name, or the default value

isNull

Syntax

If the related value for the supplied name is JsonValue.NULL, this function returns true. Parameters:

  • name - the name whose associated value is being examined. Returns: True if the associated value is JsonValue.NULL else false Throws:
  • NullPointerException - if the provided name does not contain any mappings

JsonArray getJsonArray(String name)

Returns the array value to which the specified name is mapped. This is a convenience method for (JsonArray)get(name) to get the value.

Parameters: name - the name whose associated value is to be returned

Returns: the array value to which the specified name is mapped, or null if this object contains no mapping for the name

Throws: ClassCastException - if the value to which the specified name is mapped is not assignable to the JsonArray type

put(K key, V value)

In the map, associates the supplied value with the specified key.

Parameters

  • Key - the key with which the provided value is to be associated 
  • value - the value to be associated with the specified key

Returns the previous value associated with the key, or null if no mapping for the key was found. (If the implementation allows null values, a null return might also indicate that the map already associated null with the key.)

Throws

  • UnsupportedOperationException
    • If the put operation is not supported by this map, an UnsupportedOperationException is thrown.
  • ClassCastException
    • if the supplied key or value's class precludes it from being put in this map.
  • NullPointerException
    • If the supplied key or value is null and this map does not allow null keys or values, a NullPointerException is thrown.
  • IllegalArgumentException
    • If some property of the supplied key or value precludes it from being saved in this map, an IllegalArgumentException is thrown.

Java JsonObject Examples

Example 1

In this example, we are how to check how to encode a JSON Object in java.

Output

Code Explanation In Above example, we have created JSON Object using the JSONObject() constructor. Used put() method that maps Subjects with OS,Marks with 99 Grade with A just like MAP.

Example 2

In this example we are going to find how to create JSONObject from HashMap.

Output

Code Explanation In Above code, we have created a HashMap and added some attributes of Smartphone. We have used the JSONObject(HashMap mp) Constructor to create JSONObject from HashMap.

Example 3

In this example, we are going to find how to create JSON Array in java and insert elements in JSON Array.

Code Output

Code Explanation In Above code, we have created a JSON Array and inserted some JSONObject using the put() method.

Example 4

In this example we are going to use some of method of jsonobject.

Output

Code Explanation In this code, we have created a JsonArray and added some JsonObjects to the JsonArray. Then we used methods like getJsonArray(), and getJsonObject.

Conclusion

  • A JSONObject is a collection of name/value pairs that are not ordered. The string's external form is enclosed in curly braces and has commas between values and names and colons between names and values.
  • We can read and write JSON data in Java using JSON Object in java.
  • Entities are mapped from the right to the left in encoding and when decoding or parsing, they are mapped from the left to the right.
  • JSONObject in java provides various methods

See Also: