1

I have two java applications. In both of them I am iterating json array using foreach:

JSONArray logList = jobj.getJSONArray("creation_time");
for (Object log : logList) {

}

In one project there is no error, however in the other there is a red line under logList with the error Can only iterate over an array or an instance of java.lang.Iterable

I have the same imports in both projects and I am using Java 8. I am really confused why this is happening.

7
  • Please show your imports of both classes. Commented Aug 3, 2018 at 5:19
  • 4
    Assuming you are using this class, that would not work because it does not implement java.lang.Iterable. Commented Aug 3, 2018 at 5:20
  • 1
    A for each loop need Object implement Iterable I suggest you to use a standard for. Commented Aug 3, 2018 at 5:21
  • @Logan I am using import org.json.JSONArray in both projects Commented Aug 3, 2018 at 5:31
  • @EL323 Yes, the JavaDoc is for org.json.JSONArray. Commented Aug 3, 2018 at 5:33

3 Answers 3

3

I bet the libraries you are using are different:

For library

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

the JSONArray doesn't implement iterator, thus you need for-loop like below:

for (int i = 0; i < logList.length(); i++) {
            //your operation
}

If you are using below library

<dependency>
   <groupId>com.googlecode.json-simple</groupId>
   <artifactId>json-simple</artifactId>
   <version>1.1</version>
</dependency>

Then use below approach:

        for (Object jsonObject : logList) {
            if(jsonObject instanceof JSONObject)
            {
                //your operation
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Turns out I had a json jar added externally in one project (where I was getting the error). I removed the jar and now I can iterate using foreach. Thanks for the help.
2

If org.json.JSONArray is the JSONArray you are refering.

https://developer.android.com/reference/org/json/JSONArray

JSONArray extends Object. For each loop, as the exception suggest, only support Iterable or array. Iterable is an interface. JSONArray do not implement that. JSONArray clearly is not a array. So neither case fail, thus the error shows.

If you need to loop through its element, you could

for (int i = 0; i < logList.length(); i++) {
    Object log = logList.get(i);
}

3 Comments

What's confusing me is that in one project foreach on JSONArray is working
Really? I would like to take a look. I guess, maybe they both named JSONArray but actually came from different package?
Or different version of JSONArray.
0

The other posters are correct about JSONArray not being iterable, but to answer your specific question...

IDE's can vary the level of error messaging on a per project basis depending on

  1. The level of JDK
  2. The compatibility level for the JDK
  3. Any java annotations for the class or method
  4. Java settings on the IDE itself (e.g. with Eclipse you can specify whether each compiler "issue" is an error, a warning or something to be ignored
  5. And probably other factors

Try copying the no-error section of code into the other class (with any changes to make it compile) to see if the IDE begins to flag that code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.