All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Value From JSON Object In Java Example

Last Updated : Jul 1, 2023

How To Get Value From JSON Object In Java Example

In this article we will show you the solution of how to get value from JSON object in java example, an exchange format for data, JSON is a format for encoding and decoding data. A key attribute of this format is its lightweight and linguistic independence.

A JSON to XML conversion can be performed using this tool.

The JSON package library in Java offers a wide selection of JSON packages. Using these packages, we are able to retrieve or obtain values from JSONObjects.

We have used two different JSON packages to construct our example, org.json and org.json.simple.

The purpose of this section is to show you how to retrieve values from JSONObjects in Java using a proper example.

We will now discuss the idea of how to get value from json object in java with an example.

Step By Step Guide On How To Get Value From JSON Object In Java Example :-

import org.json.*;
public class TalkersCode {
public static void main(String[] args) throws JsonException
{
JSONObject obj = new JSONObject(
"{" +
"Employee ID: 092789," +
"Employee Name:Harry Potter," +
"Age: 27, " +
"Designation: Developer," +
"City: Australia," +
"Salary: 6700000.00, " +
"Experience: 10 " +
"}"
);
System.out.println("Employee ID: "+obj.getInt("Employee ID"));
System.out.println("Employee Name: "+obj.getString("Employee Name"));
System.out.println("Age: "+obj.getInt("Age"));
System.out.println("Designation: "+obj.getString("Designation"));
System.out.println("City: "+obj.getString("City"));
System.out.println("Salary: "+obj.getDouble("Salary"));
System.out.println("Experience: "+obj.getInt("Experience") +" Months");
}
}
  1. The JSON package is imported for us to be able to use methods and objects from the JSON format.
  2. The first thing we need to do is create a class called "TalkersCode".
  3. The main method of this class contains a method-level exception called "throws JsonException," which needs to be handled.
  4. Our employee is represented by a JSONObject called "obj" which contains the details of the employee.
  5. During constructor execution, we pass in the Employee's details as a string in JSON format.
  6. Printing each employee's details to the console is done using the "println" method.
  7. Our approach is to use the keys of each of the details as parameters for the getInt, getString, or getDouble methods of the JSONObject called "obj" and print out their details to the console with "println".

Conclusion :-

As a result, we have successfully learned how to get value from json object in java with an example.

There is no ordering in the collection of name-value pairs represented by the JSONObject.

In the external form of a string, values are separated from names by commas and names are separated by colons.

Java supports reading and writing JSON data through JSON Objects. Encoding and decoding work by mapping entities from right to left, while parsing and encoding work by mapping entities left to right. There are various methods provided by the JSONObject in Java.

I hope this article on how to get value from JSON object in java example helps you and the steps and method mentioned above are easy to follow and implement.