Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Iterate through a JSONArray

I'm writing an Android app using Kotlin and Realm. I have a JSONArray, and I want to iterate through the JSONObjects in this array in order to load them in a Realm database class:

Realm class:

import io.realm.RealmObject import io.realm.annotations.PrimaryKey import io.realm.annotations.Required  open class Person(          @PrimaryKey open var id: Long = 0,          @Required         open var name: String = ""  ) : RealmObject() {  } 

The JSONArray:

{     "persons":[         {            "id":0,            "name":"Biatrix"         },         {            "id":1,            "name":"Bill"         },         {            "id":2,            "name":"Oren"         },         {            "id":3,            "name":"Budd"         }     ] } 

I've tried iterating like the following:

for (item : JSONObject in persons) {  } 

... but I get a for-loop range must have an iterator() method error.

like image 412
Ambran Avatar asked Mar 23 '16 17:03

Ambran


People also ask

How do I iterate through a JSONArray?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

How do you iterate through a JSON object Kotlin?

This example demonstrates how to iterate a JSON Array in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How to iterate a JSON array in Android using Kotlin?

This example demonstrates how to iterate a JSON Array in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.kt

How to iterate through a range in Kotlin for loop?

The syntax of for loop in Kotlin is: Here, the loop iterates through the range and prints individual item. If the body of the loop contains only one statement (like above example), it's not necessary to use curly braces { }. It's possible to iterate through a range using for loop because ranges provides an iterator.

How to iterate a jsonarray with a for statement?

Even if some class doesn't expose an iterator method, you can still iterate it with for statement by providing an extension function iterator: Now when you use JSONArray in for statement this extension is invoked to get an iterator. It creates a range of indices and maps each index to an item corresponding to this index.

How to go through an iterable collection in Python?

Another way to go through an Iterable collection is the well-known for loop. When using for on a collection, you obtain the iterator implicitly. So, the following code is equivalent to the example above: Finally, there is a useful forEach () function that lets you automatically iterate a collection and execute the given code for each element.


1 Answers

Unfortunately, JsonArray does not expose an iterator. So you will have to iterate through it using an index range:

for (i in 0 until persons.length()) {     val item = persons.getJSONObject(i)      // Your code here } 
like image 68
0x60 Avatar answered Oct 24 '22 01:10

0x60