Upload the contents of the json file to an array

Viewed 66

It is necessary to get access to ISON on URL.

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
JSONArray array = new JSONArray(loadJSONFromAsset(context, "url"));

But how to help please?

1 Answers

You should be able to do that by creating an ArrayList. Iterating thru the elements of the JSONArray and adding them in the ArrayList. You can't do this with arrays as you can't modify them.

Here's an example:

import java.util.ArrayList;
import org.json.JSONArray;

public static ArrayList<Object> getArrayListFromJSONArray(JSONArray jsonArray) throws JSONException{
    ArrayList<Object> arrayList = new ArrayList<>();
        
    for(int i = 0; i < jsonArray.length(); i++){
        Object element = jsonArray.get(i);
        arrayList.add(element);
    }            
    return arrayList;
}
Related