How To Iterate Through A Json Object In Java
Disclosure: This article may contain chapter links. When you purchase, we may earn a small committee.
How to iterate over JSONObject in Java to print all key values? Case Tutorial
Hello guys, if y'all are wondering how to iterate over a JSONObject in Java and impress all its fields and values then you have come to the right place. In this article, I will show you how you tin impress all data from JSONObject in Coffee. If you know, In json-simple library, ane of the superlative v and lightweight JSON library, JSON object is a wrapper class which contains the actual JSON bulletin. In the final article, I take explained how to parse JSON in Java using json-simple library and i question which pops up can we iterate over JSONObject properties? Well, yes, you lot can iterate over all JSON properties and besides get the values from the JSONObject itself. In this article, I'll show you how you tin can print all keys and value from JSON message using JSONOjbect and Iterator.
Java Plan to loop over JSONObject and print all properties
One time you parse your JSON Cord using JSONParser, you become a JSONObject, but its of no utilize if y'all want a POJO i.e. a plain former Java object. For case, 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 }
class EBook { private Cord title; individual String writer; private int toll; private int twelvemonth; individual int edition; private int ratings .... }
I accept omitted constructor, getter and setter, and essential methods like equals(), hashcode(), and toString() for brevity but you must recollect to include a no-statement constructor while creating POJO classes in Java, json-simple might non need that but other JSON parsing library like Jackson and Gson definitely need that.
Anyway, Here is my sample Java program to demonstrate how y'all can loop over a JSONObject to print all properties and their values.
public static void principal (Cord args[]){ // Iterating over JSONOjbect to print all keys and values JSONParser parser = new JSONParser(); endeavour { JSONObject json = (JSONObject) parser. parse (JSON); Set<String> keyset = json. keySet (); Iterator<Cord> keys = keyset. iterator (); while (keys. hasNext ()){ String key = keys. side by side (); Object value = json. go (key); Organisation. out . println ( fundamental + " : " + value); } } catch (ParseException e) { e. printStackTrace (); } } }
You lot can see that the parse() method return the states a JSONObject. This object is similar a map, and then if you lot need to iterate over all properties, you can inquire for set of keys. Similar to coffee.util.Map information technology provides a keySet() method which render a Set<String>.
We tin iterate over this Gear up to retrieve all keys as shown below:
Iterator<String> keys = keyset. iterator (); while (keys. hasNext ()){ String primal = keys. next (); Object value = json. get (fundamental); System. out . println ( central + " : " + value); }
Now, if you need value y'all can simply call the JSONObject.become() or JSONObject.getOrDefault() method to go the value. In one case you got both primal and value, you can simply print them using Arrangement.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 Coffee to print all keys and values. It'south very like to how you iterate over a map to print all key-value pair. You but need to remember to include the json-simple.jar in your classpath if you want to procedure your JSON Message like this.
Source: https://javarevisited.blogspot.com/2022/12/how-to-iterate-over-jsonobject-in-java.html
0 Response to "How To Iterate Through A Json Object In Java"
Post a Comment