Hello guys, if you are wondering how to iterate over a JSONObject in Java and print all its fields and values then you have come to the right place. In this article, I will show you how you can print all data from JSONObject in Java. If you know, In json-simple library, one of the top 5 and lightweight JSON library, JSON object is a wrapper class which contains the actual JSON message. In the last article, I have explained how to parse JSON in Java using json-simple library and one question which pops up can we iterate over JSONObject properties? Well, yes, you can iterate over all JSON properties and also get the values from the JSONObject itself. In this article, I'll show you how you can print all keys and value from JSON message using JSONOjbect and Iterator.
Java Program to loop over JSONObject and print all properties
Once you parse your JSON String using JSONParser, you get a JSONObject, but its of no use if you want a POJO i.e. a plain old Java object. For example, if you have following JSON mesage which represent Effective Java book, you ideall want a Java object representing same data:
{ "title": "Effective Java", "author": "Joshua Bloch", "price": 42, "year": 2018, "edition": 3, "ratings": 10 }
If you remember we had created an EBook object in our last example about paring JSON with date field in Java, similar to following class:
class EBook{ private String title; private String author; private int price; private int year; private int edition; private int ratings .... }
I have omitted constructor, getter and setter, and essential methods like equals(), hashcode(), and toString() for brevity but you must remember to include a no-argument constructor while creating POJO classes in Java, json-simple might not need that but other JSON parsing library like Jackson and Gson definitely need that.
Anyway, Here is my sample Java program to demonstrate how you can loop over a JSONObject to print all properties and their values.
public static void main(String args[]){ // Iterating over JSONOjbect to print all keys and values JSONParser parser = new JSONParser(); try { JSONObject json = (JSONObject) parser.parse(JSON); Set<String> keyset = json.keySet(); Iterator<String> keys = keyset.iterator(); while(keys.hasNext()){ String key = keys.next(); Object value = json.get(key); System.out.println( key +" : " + value); } } catch (ParseException e) { e.printStackTrace(); } } }
You can see that the parse() method return us a JSONObject. This object is like a map, so if you need to iterate over all properties, you can ask for set of keys. Similar to java.util.Map it provides a keySet() method which return a Set<String>.
We can iterate over this Set to retrieve all keys as shown below:
Iterator<String> keys = keyset.iterator(); while(keys.hasNext()){ String key = keys.next(); Object value = json.get(key); System.out.println( key +" : " + value); }
Now, if you need value you can just call the JSONObject.get() or JSONObject.getOrDefault() method to get the value. Once you got both key and value, you can simply print them using System.out.println() method. If you want to create an object, you can store these values to pass to the constructor, as I did in last example.
That's all about how to iterate over JSONObject in Java to print all keys and values. It's very similar to how you iterate over a map to print all key-value pair. You just need to remember to include the json-simple.jar in your classpath if you want to process your JSON Message like this.
Other JSON tutorials in Java you may like
Thanks for reading this article so far. If you like this Java JSON tutorial using json-simple library then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
- How to parse JSON using Gson?
- How to format JSON String in Java?
- How to return JSON from Spring MVC controller?
- How to parse a JSON array in Java?
- How to download Jackson JAR files in Java
- How to Solve UnrecognizedPropertyException: Unrecognized field, not marked as ignorable -
- How to convert JSON to HashMap in Java?
- 10 Things Java developers should learn
- 20 JSON Interview Questions with Answers
- How to ignore unknown properties while parsing JSON in Java?
Thanks for reading this article so far. If you like this Java JSON tutorial using json-simple library then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
No comments :
Post a Comment