How to Get Value from JSON Object in Java ExampleJSON 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 ClassJava 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() MethodThe 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()MethodIt 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() MethodIt 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() MethodIt 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() MethodIt 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() MethodIt 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() MethodIt 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.GetValueFromJsonExample.java Output: 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. GetValueFromJsonObjectExample.java Output: |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India