1

I am using JSON simple to parse a JSON file. When I do so I get a JSONArray. But when I try to iterate through it and get its JSONObject elements I get an error.

This is my code:

JSONArray jsonData = (JSONArray) jsonParser.parse(reader);

List<JSONObject> elementsList = new ArrayList<JSONObject>();

for (int i = 1; i < jsonData.size(); i++) {
     elementsList.addAll(jsonData.get(i)); // Here jsonData.get(i) is a JSONObject
}

I get the following errors in Eclipse:

  • The method addAll(Collection) in the type List is not applicable for the arguments (Object)
  • Type safety: Unchecked cast from Object to Collection

Not sure what these mean and how to fix that.

3
  • What is the return type of JSONArray#get(int)? Commented Jun 4, 2015 at 15:56
  • @SotiriosDelimanolis I edited my question. It is addAll in fact. Commented Jun 4, 2015 at 16:01
  • @SotiriosDelimanolis jsonData.get(i) is a JSONObject Commented Jun 4, 2015 at 16:01

4 Answers 4

3

JSONArray#get(int) has a return type of Object (because it is inherited from the raw type ArrayList). List#addAll(Collection) expects an argument of type Collection. The type Object is not convertible to a Collection without a type cast.

However, even if you were to cast the value returned by get, the underlying value would actually be a JSONObject and you'd get a ClassCastException at runtime.

What you want is

elementsList.addAll(jsonData); //  outside the loop

since JSONArray is a subtype of ArrayList which is a Collection. You'll get a warning about jsonData requiring an unchecked conversion, but you should be good assuming that you actually have JSONObject values inside your JSONArray.

Sign up to request clarification or add additional context in comments.

Comments

0

As you wrote, json Data.get(i) is a JSONObject, thus, it's normal that you can't add it with addAll. Basically, use addAll if you want to add a List and add if you want to add an object.

You could change your code to iterate through your jsonData with your for-loop using add. On the other way, you could simply use elementsList.addAll(jsonData)

Comments

0

try like that.

for (int i = 1; i < jsonData.size(); i++) {
 elementsList.add(jsonData.getJSONObject(i)); 
}

1 Comment

jsonData doesn't have a getJSONObject method
0

The issue is, ArrayList.addAll() takes only java.util.Collection types and can't take JSONObject as input parameter,

Here is the extract from javadoc,

public boolean addAll(Collection c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)

In order to get all the JSONObjects into the list, use add method as mentioned below insteadof addAll(),

JSONArray jsonData = (JSONArray) jsonParser.parse(reader);

List<JSONObject> elementsList = new ArrayList<JSONObject>();

for (int i = 1; i < jsonData.size(); i++) {
    elementsList.add(jsonData.get(i)); // Here jsonData.get(i) is a JSONObject
}

2 Comments

I tried to use add as well however it didn't work either. The compiler error in this case is The method add(JSONObject) in the type List<JSONObject> is not applicable for the arguments (Object)
Ah I see.. If you are pretty sure that all the elements within JSONArray is going to be JSONObject, then use jsonData.getJSONObject(index) method insteadof jsonData.get(index)

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.