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,190 points
19,341 views

10 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,280 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

Related Questions In Java

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,050 points
63 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,190 points
189 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,880 points
62 views
0 votes
2 answers

How to iterate in a HashMap in Java?

Iterating using Iterator. Using Generics: Map<Integer, Integer> map = ...READ MORE

answered Aug 28 in Java by Sirajul
• 39,540 points
62 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 351 views
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,280 points
32 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 in Java by Sirajul
• 39,540 points
91 views