Sunday 21 May 2023

How to iterate over JSONObject in Java to print all key values with Example

Working with JSON data is a common task in many Java applications, and being able to iterate over a JSONObject to extract and print all key-value pairs is often necessary. In this tutorial, we will explore various methods to accomplish this in Java.

Before we begin, make sure you have the necessary JSON library. There are several options available, such as JSON.simple, Gson, or Jackson. For the purpose of this tutorial, we will use the JSON.simple library, which is lightweight and easy to use.

We will parse below JSON,

{
    "name""John Doe",
    "age""30",
    "city""New Jersey"
}

Import the JSON.simple Library to get started, you need to import the JSON.simple library into your project. If you are using Maven, add the following dependency to your project's pom.xml file.

xml
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>

If you are not using Maven, you can download the JAR file from the JSON.simple GitHub repository and add it to your project manually.

parse the provided JSON using JSONParser and print all the key sets using an iterator:


java
import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.util.Iterator; public class JSONParsingExample { public static void main(String[] args) { String jsonString = "{\"name\":\"John Doe\",\"age\":\"30\",\"city\":\"New Jersey\"}"; // Parse the JSON string JSONParser parser = new JSONParser(); try { Object obj = parser.parse(jsonString); JSONObject jsonObject = (JSONObject) obj; // Get an iterator for the key set Iterator iterator = jsonObject.keySet().iterator(); // Iterate and print all key sets while (iterator.hasNext()) { String key = (String) iterator.next(); System.out.println(key); } } catch (ParseException e) { e.printStackTrace(); } } }

In this example, after parsing the JSON string, we obtain an iterator for the key set of the JSONObject using jsonObject.keySet().iterator(). We then iterate through the key set using a while loop and retrieve each key using iterator.next(). We cast each key to a String and print it using System.out.println(key).

When you run this program, you will see the following output:

name age city
Each key in the JSON object will be printed on a separate line, just as before.

If you want to print values you can use below,

java
// Get an iterator for the values Iterator iterator = jsonObject.values().iterator(); // Iterate and print all values while (iterator.hasNext()) { Object value = iterator.next(); System.out.println(value); }

When you run this program, you will see the following output:

output
John Doe 30 New Jersey

So you can effectively iterate over a JSONObject in Java and extract the desired information from your JSON data. Please let us know your questions in comment section.


No comments:

Post a Comment

Seven front-end development trends in 2023-2024

With the increasing prevalence of apps in the digital landscape , the role of front-end developers remains essential. While apps aim to ove...

Popular Posts