List to JSONArray java

I have an ArrayList that I use within an ArrayAdapter for a ListView. I need to take the items in the list and convert them to a JSONArray to send to an API. I've searched around, but haven't found anything that explains how this might work, any help would be appreciated.

UPDATE - SOLUTION

Here is what I ended up doing to solve the issue.

Object in ArrayList:

public class ListItem { private long _masterId; private String _name; private long _category; public ListItem[long masterId, String name, long category] { _masterId = masterId; _name = name; _category = category; } public JSONObject getJSONObject[] { JSONObject obj = new JSONObject[]; try { obj.put["Id", _masterId]; obj.put["Name", _name]; obj.put["Category", _category]; } catch [JSONException e] { trace["DefaultListItem.toString JSONException: "+e.getMessage[]]; } return obj; } }

Here is how I converted it:

ArrayList myCustomList = .... // list filled with objects JSONArray jsonArray = new JSONArray[]; for [int i=0; i < myCustomList.size[]; i++] { jsonArray.put[myCustomList.get[i].getJSONObject[]]; }

And the output:

[{"Name":"Name 1","Id":0,"Category":"category 1"},{"Name":"Name 2","Id":1,"Category":"category 2"},{"Name":"Name 3","Id":2,"Category":"category 3"}]

Hope this helps someone some day!

In Java, JSON plays an important role in storing data. ArrayList is a special type of Array whose size is dynamic. It is also used to store or remove data anytime. ArrayList uses all the method of List and maintain the insertion order because it implements the List Interface. In some cases, we get the data in the JSON format and to work on it, we need to convert it into an Array-like structure. ArrayList is the best way to access data, and beginners are mostly asked how to convert JSON Array to ArrayList.

The server returns the response in JSON object or JSON Array, and to work on it efficiently, we need to convert it into an Array-like structure, e.g., ArrayList. The process of converting JSON Array into ArrayList is quite simple. We just need to iterate the JSON Array and add each element of the Array to the ArrayList.

In order to convert the JSON Array to ArrayList

  1. We will use the Maven because it is as easy as adding a dependency.
  2. We will add the following dependency to work with JSONArray and JSONObject.

org.json json 20180813

  1. We will convert the JSON string into a JSON object.
  2. We will get the JSON array from the JSON object.
  3. We will create an empty array list of type Object.
  4. After that, we will iterate the JSON Array to push each element of it into ArrayList.

Let's see an example to understand how to convert the JSON Array into ArrayList.

We have the following json string from which we read the JSON data.

String json = "[{"name": "Java", "description": "Java is a class-based high-level programming language that follows the OOPs concepts."}, { "name": "Javascript", "description": "JavaScript is also a high-level, often just-in-time compiled, and multi-paradigm programming language." }, { "name": "Python", "description": "Python is another high-level, interpreted and general-purpose programming language." }]"

JSONArrayToArrayList.java

//Import packages and classes package JavaTpoint.JavaObjectToJSON; import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; //Creating JSONArrayToArrayList class public class JSONArrayToArrayList { public static void main[String[] args]{ //Creating string of JSON data String jsonData = "{\"languages\" : [{\"name\": \"Java\", \"description\":" + " \" Java is a class-based high-level programming language that" + " follows the OOPs concepts.\"},{\"name\": \"Javascript\"," + "\"description\": \"JavaScript is also a high-level, often " + "just-in-time compiled, and multi-paradigm programming language." + "\"},{\"name\": \"Python\",\"description\": \"Python is another " + "high-level, interpreted and general-purpose programming language." + "\"}]}"; //Converting jsonData string into JSON object JSONObject jsnobject = new JSONObject[jsonData]; //Printing JSON object System.out.println["JSON Object"]; System.out.println[jsnobject]; //Getting languages JSON array from the JSON object JSONArray jsonArray = jsnobject.getJSONArray["languages"]; //Printing JSON array System.out.println["JSON Array"]; System.out.println[jsonArray]; //Creating an empty ArrayList of type Object ArrayList listdata = new ArrayList[]; //Checking whether the JSON array has some value or not if [jsonArray != null] { //Iterating JSON array for [int i=0;i System.out.println[x.toString[]]];

The langs.json file contains:

[ { "name": "Java", "description": "Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible." }, { "name": "Python", "description": "Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991." }, { "name": "JS", "description": "JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm." } ]

Running this code results in:

Language{name='Java', description='Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.'} Language{name='Python', description='Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991.'} Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.'}

Conclusion

In this article, we've used Jackson to parse and map the values from a JSON String and file into a Java array and list.

This is done via the readValue[] method, by specifying the JSON contents [String or file] and by specifying the POJO we'd like to map to.

Video liên quan

Chủ Đề