Eugene S
Eugene S

Reputation: 6910

Can't add JSONObjects into an array

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:

Not sure what these mean and how to fix that.

Upvotes: 1

Views: 1966

Answers (4)

Sathish
Sathish

Reputation: 5173

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
}

Upvotes: 0

vikas balyan
vikas balyan

Reputation: 806

try like that.

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

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

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.

Upvotes: 3

Gary SEBASTIANI
Gary SEBASTIANI

Reputation: 302

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)

Upvotes: 0

Related Questions