How you iterate and add

 for(int i=0;i<jsonArray.length;i++)
    {    
         JSONObject json_data = jArray.getJSONObject(i);
         imgnfo.id =  json_data.getInt("id_key");
         imgnfo.name = json_data.getString("name_key");
         imgnfo.thumb = json_data.getString("thumb_key");
         imgnfo.info = json_data.getString("info_key");
         myArray.add(new image_data());
    }

Just add proper keys names. I didn't know them.

Answer from Nikola Despotoski on Stack Overflow
Discussions

java - Put ArrayList into param JsonObject - Stack Overflow
i must do i request with Volley Framework. This is a POST request with JSONObject. I must pass one string and one JSONArray..but how i can? I start with this: private String mUrl; private More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Creating an ArrayList from a JSON Object - Stack Overflow
So I have a JSON just like the picture below stored as MYJSON-- My plan is to retrieve all the object from childObject0 and store them in an ArrayList so they can be processed later. So I did- Arr... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to convert ArrayList to JSON object - Stack Overflow
ArrayList mylist = new ArrayList (); mylist.add("abc"); mylist.add("cfd"); mylist.add("ert"); mylist.add("fg"); mylist.add("ujk"); String json = new Gson().toJson(mylist); You can refer to the GSON User Guide for more support. ... Sign up to request clarification or add additional context in comments. ... i am trying to convert the array into json object... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 7, 2014
Json object to java arraylist - Stack Overflow
1 (Gson) JSON to Java objects containing ArrayList More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Reddit
reddit.com โ€บ r/androiddev โ€บ how do i save an arraylist with json?
r/androiddev on Reddit: How do I save an ArrayList<(Custom object here)> with JSON?
July 21, 2014 -
public class Semester {    
private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_CREDITS = "credits";
private static final String JSON_GPA = "gpa";
private static final String JSON_CUMULATIVECREDITS = "cumulativecredits";
private static final String JSON_CUMULATIVEGPA = "cumulativegpa";
private static final String JSON_COURSES = "courses";

private UUID id;
private String title;
private int credits;
private double GPA;
private int cumulativeCredits;
private double cumulativeGPA;

private ArrayList<Course> courses;

public Semester() {
    id = UUID.randomUUID();
    courses = new ArrayList<Course>();
    for (int i = 0; i < 6; i++) {
        courses.add(new Course());
    }
}

public Semester(JSONObject json) throws JSONException {
    id = UUID.fromString(json.getString(JSON_ID));
    title = json.getString(JSON_TITLE);
    credits = json.getInt(JSON_CREDITS);
    GPA = json.getDouble(JSON_GPA);
    cumulativeCredits = json.getInt(JSON_CUMULATIVECREDITS);
    cumulativeGPA = json.getDouble(JSON_CUMULATIVEGPA);

//what do i put here?

}

public JSONObject toJSON() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(JSON_ID, id.toString());
    json.put(JSON_TITLE, title);
    json.put(JSON_CREDITS, credits);
    json.put(JSON_GPA, GPA);
    json.put(JSON_CUMULATIVECREDITS, cumulativeCredits);
    json.put(JSON_CUMULATIVEGPA, cumulativeGPA);

JSONArray array = new JSONArray(courses);

json.put(JSON_COURSES, array);

    return json;
}

.........

This is followed by a bunch of getters and setters for each private variable.

  • The first bolded item is the private ArrayList of a custom class that I made.

  • The second bolded item is what I need help with: how to retrieve the custom ArrayList just like the other variables.

  • Lastly, the third bolded item is my current idea of how to put an ArrayList into a JSONObject, but I don't know how to retrieve it. If there is another way, please tell me. Thank you.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-we-add-a-jsonarray-to-jsonobject-in-java
How can we add a JSONArray to JSONObject in Java?
July 4, 2020 - We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. import org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest ...
๐ŸŒ
Crunchify
crunchify.com โ€บ json tutorials โ€บ in java how to convert arraylist to jsonobject?
In Java How to Convert ArrayList to JSONObject? โ€ข Crunchify
May 17, 2018 - Gson gson = gsonBuilder.create(); String JSONObject = gson.toJson(crunchify); log("\nConverted JSONObject ==> " + JSONObject); Gson prettyGson = new GsonBuilder().setPrettyPrinting().create(); String prettyJson = prettyGson.toJson(crunchify); log("\nPretty JSONObject ==> " + prettyJson); } private static void log(Object print) { System.out.println(print); } } Raw ArrayList ===> [Google, Facebook, Crunchify, Twitter, Snapchat, Microsoft] Converted JSONObject ==> ["Google","Facebook","Crunchify","Twitter","Snapchat","Microsoft"] Pretty JSONObject ==> [ "Google", "Facebook", "Crunchify", "Twitter", "Snapchat", "Microsoft" ] I hope you can use this simple utility to convert any Java Objects to JSONObject or JSONArray.
Find elsewhere
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2024 โ€บ 06 โ€บ convert-json-array-to-arraylist-in-java
Convert JSON Array to ArrayList in Java
June 13, 2024 - import org.json.JSONArray; import ... jsonArray = new JSONArray(jsonArrayString); // Convert JSONArray to ArrayList ArrayList<JSONObject> arrayList = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { arrayList.add(jsonArray.getJSONObject(i)); } // Print the ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-convert-json-array-to-arraylist-in-java
How to Convert JSON Array to ArrayList in Java - Javatpoint
How to Convert JSON Array to ArrayList in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 60019377 โ€บ how-to-make-a-arraylist-with-json-objects-in-java
How to make a Arraylist with JSON Objects in JAVA? - Stack Overflow
It's generally worth doing a search for common programming tasks. You could just google search "java add to list" which should result in something like: List<JSONObject> myList = new ArrayList<>(); myList.add(uptimeResponsObject);
๐ŸŒ
YouTube
youtube.com โ€บ watch
Java json 01 create an arraylist to serialize with Jackson library - YouTube
Shows how to prepare an arraylist of a class. Preparing for use of a json library called Jackson. Part 1 of 3 for how to read and write json files using java
Published: January 29, 2019
Top answer
1 of 16
204
ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
} 
2 of 16
85

I've done it using Gson (by Google).

Add the following line to your module's build.gradle:

dependencies {
  // ...
  // Note that `compile` will be deprecated. Use `implementation` instead.
  // See https://stackoverflow.com/a/44409111 for more info
  implementation 'com.google.code.gson:gson:2.8.2'
}

JSON string:

private String jsonString = "[\n" +
            "        {\n" +
            "                \"id\": \"c200\",\n" +
            "                \"name\": \"Ravi Tamada\",\n" +
            "                \"email\": \"ravi@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c201\",\n" +
            "                \"name\": \"Johnny Depp\",\n" +
            "                \"email\": \"johnny_depp@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c202\",\n" +
            "                \"name\": \"Leonardo Dicaprio\",\n" +
            "                \"email\": \"leonardo_dicaprio@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c203\",\n" +
            "                \"name\": \"John Wayne\",\n" +
            "                \"email\": \"john_wayne@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c204\",\n" +
            "                \"name\": \"Angelina Jolie\",\n" +
            "                \"email\": \"angelina_jolie@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c205\",\n" +
            "                \"name\": \"Dido\",\n" +
            "                \"email\": \"dido@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c206\",\n" +
            "                \"name\": \"Adele\",\n" +
            "                \"email\": \"adele@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c207\",\n" +
            "                \"name\": \"Hugh Jackman\",\n" +
            "                \"email\": \"hugh_jackman@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c208\",\n" +
            "                \"name\": \"Will Smith\",\n" +
            "                \"email\": \"will_smith@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c209\",\n" +
            "                \"name\": \"Clint Eastwood\",\n" +
            "                \"email\": \"clint_eastwood@gmail.com\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-java
How to convert Java array or ArrayList to JsonArray using Gson in Java?
July 8, 2020 - import com.google.gson.*; import java.util.*; public class JavaArrayToJsonArrayTest { public static void main(String args[]) { String[][] strArray = {{"elem1-1", "elem1-2"}, {"elem2-1", "elem2-2"}}; ArrayList<ArrayList<String>> arrayList = new ArrayList<>(); for(int i = 0; i < strArray.length; i++) { ArrayList<String> nextElement = new ArrayList<>(); for(int j = 0; j < strArray[i].length; j++) { nextElement.add(strArray[i][j] + "-B"); } arrayList.add(nextElement); } JsonObject jsonObj = new JsonObject(); // array to JsonArray JsonArray jsonArray1 = new Gson().toJsonTree(strArray).getAsJsonArray(); // ArrayList to JsonArray JsonArray jsonArray2 = new Gson().toJsonTree(arrayList).getAsJsonArray(); jsonObj.add("jsonArray1", jsonArray1); jsonObj.add("jsonArray2", jsonArray2); System.out.println(jsonObj.toString()); } }
๐ŸŒ
YouTube
youtube.com โ€บ watch
Java JSON Array Encode Using ArrayList - YouTube
#JavaJSONArray #HomeStay #StaySafe #ArrayList #JSONArrayListJava JSON ArrayList Encode0:05 JSON Install1:53 JSON Array Example Start time:Like, Share, And Su...
Published: April 7, 2020