Iterate over a JSONObject

+1 vote

JSON library called JSONObject is used(I don't mind switching if I need to)

We know how to iterate over JSONArrays, but when we parse JSON data from Facebook we don't get an array, only a JSONObject, but I need to be able to access an item via its index, such as JSONObject[0] to get the first one, How can we do it?

{
   "http://http://url.com/": {
      "id": "http://http://url.com//"
   },
   "http://url2.co/": {
      "id": "http://url2.com//",
      "shares": 15
   }
   ,
   "http://url3.com/": {
      "id": "http://url3.com//",
      "shares": 15
   }
}
Jun 27, 2018 in Java by samarth295
• 2,220 points
124,784 views

12 answers to this question.

0 votes

There is no more simple and secure solution than using an iterator.

JSONObject names () method returns a JSONArray of the JSONObject keys, so you can simply walk though it in loop:

JSONObject objects = new JSONObject ();
JSONArray key = objects.names ();
for (int i = 0; i < key.length (); ++i) {
   String keys = key.getString (i); 
   String value = objects.getString (keys);
}
answered Jun 27, 2018 by scarlett
• 1,290 points
0 votes

to iterate over a json file you can use the following code:

jObj = new JSONObject(contents.trim());
Iterator<String> keys = jObj.keys();
while(keys.hasNext()) {
    String key = keys.next();
    if (jObject.get(key) instanceof JSONObject) {
    }
}
answered Dec 7, 2018 by Nitesh
0 votes
for(int i = 0; i<jobject.names().length(); i++){
    Log.v(TAG, "key = " + jobject.names().getString(i) + " value = " + jobject.get(jobject.names().getString(i)));
}

I used this to iterate over names

answered Dec 7, 2018 by Neha
0 votes
Iterator<JSONObject> itr = jsonObject.values().itr();
while (itr.hasNext()) {
 jsonChildObject = itr.next();
 // Do whatever you want with ChildObject 
  String id = (String) jsonChildObject.get("id");
}
answered Dec 7, 2018 by Kalgi
0 votes
JSONObject obj = new JSONObject ();
JSONArray key = object.names ();

for (int i = 0; i < key.length (); ++i) {

   String key = key.getString (i); // Here's your key
   String value = obj.getString (key); // Here's your value

}
answered Dec 7, 2018 by simplecoding
0 votes

Simple method of iterating over the json file is 

for(String key : jsonObject.keySet())
answered Dec 7, 2018 by Bobin
0 votes

JSONObject names () method returns a JSONArray of the JSONObject keys, so you can iterate it in loop:

JSONObject createobject = new JSONObject ();
JSONArray getkeys = object.names ();
for (int i = 0; i < getkeys.length (); ++i) {
   String key = getkeys.getString (i); // Here's your key
   String value = createobject.getString (key); // Here's your value
}
answered Dec 7, 2018 by Rummba
0 votes
jsonOb = new JSONObject();
Iterator<String> keys = jsonOb.keys();
while(keys.hasNext()) {
    String key = keys.next();
    if (jObject.get(key) instanceof JSONObject) {
    }
}
answered Dec 7, 2018 by abc
0 votes

Use for loop

for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    console.log(key + ": " + obj[key]);
  }
}
answered Dec 7, 2018 by test
0 votes

You can use the following method to itterate over json file

json_object = json.loads(json_file)
for element in json_object: 
    for value in json_object['Name_OF_YOUR_KEY/ELEMENT']:
        print(json_object['Name_OF_YOUR_KEY/ELEMENT']['INDEX_OF_VALUE']['VALUE'])
answered Dec 7, 2018 by Shuvodip
0 votes

Iterate through JSONObject in java (android)

  1. String jsonstring = "{ "child": { "something": "value", "something2": "value" } }";
  2. JSONObject resobj = new JSONObject(jsonstring);
  3. Iterator<?> keys = resobj. ...
  4. while(keys. hasNext() ) {
  5. String key = (String)keys. next();
  6. if ( resobj. get(key) instanceof JSONObject ) {
  7. JSONObject xx = new JSONObject(resobj. ...
  8. Log.

To iterate through JSONArray?

Here you go

getJSONObject("JObjects"); JSONArray getArray = getObject. getJSONArray("JArray1"); for(int i = 0; i < getArray. size(); i++) { JSONObject objects = getArray. getJSONArray(i); //Iterate through the elements of the array i. //Get thier value. //Get the value for the first element and the value for the last element. }

answered Dec 11, 2020 by Gitika
• 65,770 points
0 votes

Assuming your JSON object is saved in a file "simple.json", you can iterate over the attribute-value pairs as follows:

JSONParser parser = new JSONParser();

Object obj = parser.parse(new FileReader("simple.json"));

JSONObject jsonObject = (JSONObject) obj;

for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
    String key = (String) iterator.next();
    System.out.println(jsonObject.get(key));
}
answered Dec 11, 2020 by Rajiv
• 8,870 points

Related Questions In Java

0 votes
1 answer

How do I efficiently iterate over each entry in a Java Map?

To answer your questions, use the following ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,710 points
640 views
0 votes
0 answers

Ho do I Iterate through a HashMap which contains duplicate values

What is the fastest and the best ...READ MORE

Apr 16, 2018 in Java by Daisy
• 8,140 points
810 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
5,293 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,920 points
1,108 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
669 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,848 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP