Javatpoint Logo
Javatpoint Logo

How to Get Value from JSON Object in Java Example

JSON is a data-interchange format. It is a widely used light-weight and language-independent format. It is capable to convert data from JSON to XML. Java provides a large number of JSON packages. With the help of these packages, we can retrieve or get value from JSONObject. We have used the two different JSON packages org.json and org.json.simple. In this section, we will learn how to get values from JSONObject in Java with a proper example.

What is JSONObject in Java?

In Java, JSONObject is an unordered collection that contains data in form of name-value pairs. It is denoted as the string enveloped in a pair of curly braces with colons between the names and values. Its internal form is an object having to get and put methods for accessing the values by name and adding and replacing values by name.

In the above example, first name, city, and age are names, and John, Florida, and 22 are their values, respectively.

Java JSONObject Class

Java provides the JSONObject class that is defined in the org.json package. It denotes an immutable (unchallengeable) JSONObject value. The object value is an unordered group of name-value pairs. It also provides Map to view the JSON object name-value mapping that is unmodifiable. The JSONObject values can be Boolean, String, Number, JSONObject, JSONArray, JSONNumber, JSONString, etc. These JSONObject values can be accessed by using the various corresponding accessor methods.

The get() method returns a value if it founds and throws an exception if the value not found. While the opt() method returns the default value instead of throwing the exception. The value may be null. It is the main difference between these two methods. Hence, these two methods are useful to get the value from the JSONObject. We can cast or query an object returned by the generic methods get() and opt().

The JSONObject class contains the following methods that we can use to get values from the JSONObject.

getBoolean() Method

The method parses a parameter name of type String. It returns a Boolean value of the related mapping for the parse's parameter. It returns true if the associated mapping is JsonValue.TRUE and returns false if the associated mapping is JsonValue.FALSE.

Syntax:

It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonValue.TRUE or JsonValue.FALSE.

getInt()Method

It is used to get the JsonNumber(name).intValue(). The method parses a parameter name of type String. It returns an integer value of the associated mapping for the parsed parameter.

Syntax:

It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonNumber.

getJsonArray() Method

It is used to get the JsonArray get(name). The method parses an argument name of type String whose associated value is to be returned. It returns an array value of the associated mapping for the parseed parameter. It returns null if the object has no mapping for the parameter.

Syntax:

It throws ClassCastException the specified name mapping is not assignable to JsonArray.

getJsonNumber() Method

It is used to get the (JsonNumber)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the number value of the associated mapping for the parseed parameter. It returns null if the object has no mapping for the parameter.

Syntax:

It throws ClassCastException the specified name mapping is not assignable to JsonNumber.

getJsonObject() Method

It is used to get the (JsonObject)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the object of the associated mapping for the parse's parameter. It returns null if the object has no mapping for the parameter.

Syntax:

It throws ClassCastException the specified name mapping is not assignable to JsonObject.

getJsonString() Method

It is used to get the (JsonString)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the String value of the associated mapping for the parsed parameter. It returns null if the object has no mapping for the parameter.

Syntax:

It throws ClassCastException the specified name mapping is not assignable to JsonString.

getString() Method

It is used to get the getJsonString(name).getString(). The method parses an argument name of type String whose related value is to be returned as String. It returns the String value of the associated mapping for the parsed parameter.

Syntax:

It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonString.

Let's create a program and get value from the JSON object by using the above methods.

Note: Before using the following code, download the json-20200518.jar file and add it to your project.

How to Get Value from JSON Object in Java Example

GetValueFromJsonExample.java

Output:

How to Get Value from JSON Object in Java Example

Let's use another JSON library and get values from the JSONObject. In the following program, we have used the json-simple library. Before using the following program, download the json-simple-1.1.1.jar file and add it to your project.

How to Get Value from JSON Object in Java Example

GetValueFromJsonObjectExample.java

Output:

How to Get Value from JSON Object in Java Example





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA