Home  >  org.json

Java JSONArray Example

By Arvind Rai, May 22, 2022
On this page we will learn using org.json.JSONArray class. The org.json API handles the JSON operation in Java application.
1. A JSONArray is an ordered sequence of values.
2. The JSONArray produces a string wrapped in square brackets ([]) with commas separating the values.
3. To add the values, JSONArray provides put method.
4. To get values, JSONArray provides get methods.
5. To get the optional object value, JSONArray provides opt method.
6. To make String, JSONArray provides toString method.
7. To use org.json API in our application, we need to resolve following dependency.
<dependency>
	<groupId>org.json</groupId>
	<artifactId>json</artifactId>
	<version>20220320</version>
</dependency> 
Now let us discuss using JSONArray class.



1. Instantiate JSONArray

The JSONArray is instantiated using following constructors.
JSONArray()
JSONArray(Collection<?> collection)
JSONArray(int initialCapacity)
JSONArray(Iterable<?> iter)
JSONArray(JSONArray array)
JSONArray(JSONTokener x)
JSONArray(Object array)
JSONArray(String source) 
Example-1: Instantiating JSONArray using Collection .
List<String> list = List.of("Mahesh", "Krishna", "Brahma");
JSONArray jsonArray = new JSONArray(list);
System.out.println(jsonArray.toString()); 
Output
["Mahesh","Krishna","Brahma"] 
Example-2: Instantiating JSONArray using String array.
String[] ar = {"Mahesh", "Krishna", "Brahma"};
JSONArray jsonArray = new JSONArray(ar);
System.out.println(jsonArray.toString()); 
Output
["Mahesh","Krishna","Brahma"] 

2. Putting Values

To put values in JSONArray, we can use following methods.
a. Appends Boolean value.
JSONArray put(boolean value) 
Put or replace value using following method.
JSONArray put(int index, boolean value) 
Example
JSONArray jsonArray = new JSONArray();
jsonArray.put(true);
jsonArray.put(false);
jsonArray.put(true);
System.out.println(jsonArray.toString()); 
Output
[true,false,true] 
b. Put int value.
JSONArray put(int value) 
We can also pass double and float values.
Find the method to put or replace value.
JSONArray put(int index, int value) 
Example
JSONArray jsonArray = new JSONArray();
jsonArray.put(10);
jsonArray.put(15);
jsonArray.put(20);
System.out.println(jsonArray.toString()); 
Example
[10,15,20] 
c. Put Collection.
JSONArray put(Collection<?> value) 
Put or replace Collection for the given index.
JSONArray put(int index, Collection<?> value) 
Example
List<String> list = new ArrayList<>();
list.add("Varanasi");
list.add("Prayagraj");
list.add("Ayodhya");
JSONArray jsonArray = new JSONArray();
jsonArray.put(list);
System.out.println(jsonArray.toString()); 
Output
[["Varanasi","Prayagraj","Ayodhya"]] 
d. Put Map. First Map will produce a JSONObject using key/value. Then this instance of JSONObject will be used as value for JSONArray.
JSONArray put(Map<?,?> value) 
Put or replace Map for given index.
JSONArray put(int index, Map<?,?> value) 
Example
Map<Integer, String> map1 = new HashMap<>();
map1.put(101, "Mohit");
map1.put(102, "Suresh");
map1.put(103, "Anand");

Map<Integer, Integer> map2 = new HashMap<>();
map2.put(101, 25);
map2.put(102, 20);
map2.put(103, 30); 

JSONArray jsonArray = new JSONArray();
jsonArray.put(map1);
jsonArray.put(map2);
System.out.println(jsonArray.toString(2)); 
Output
[
  {
    "103": "Anand",
    "101": "Mohit",
    "102": "Suresh"
  },
  {
    "103": 30,
    "101": 25,
    "102": 20
  }
] 



3. Getting Values

The JSONArray has following methods for get operation.
Object get(int index)
BigDecimal getBigDecimal(int index)
BigInteger getBigInteger(int index)
boolean getBoolean(int index)
double getDouble(int index)
<E extends Enum<E>> getEnum(Class<E> clazz, int index)
float getFloat(int index)
int getInt(int index)
JSONArray getJSONArray(int index)
JSONObject getJSONObject(int index)
long getLong(int index)
Number getString(int index)
String getNumber(int index) 
Find the example.
JSONArray jsonArray1 = new JSONArray();
jsonArray1.put(10);
jsonArray1.put(15);
jsonArray1.put(20);
int intVal = jsonArray1.getInt(1);
System.out.println(intVal); // 15

JSONArray jsonArray2 = new JSONArray();
jsonArray2.put("Mahesh");
jsonArray2.put("Krishn");
jsonArray2.put("Gopal");
String strVal = jsonArray2.getString(2);
System.out.println(strVal); // Gopal 

4. toString() Method

a.
String toString() 
Make a JSON text of this JSONArray.
Ex.
JSONArray jsonArray = new JSONArray();
jsonArray.put("Mahesh");
jsonArray.put("Krishn");
jsonArray.put("Gopal");
String output = jsonArray.toString();
System.out.println(output); 
Output
["Mahesh","Krishn","Gopal"] 
b.
String toString(int indentFactor) 
Make a pretty-printed JSON text of this JSONArray. The indentFactor is the number of spaces to add to each level of indentation.
Ex.
String output = jsonArray.toString(2);
System.out.println(output);
Output
[
  "Mahesh",
  "Krishn",
  "Gopal"
] 

5. toJSONObject() Method

Find the toJSONObject method declaration.
JSONObject toJSONObject(JSONArray names) 
Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
Ex.
JSONArray ages = new JSONArray();
ages.put(10);
ages.put(15);
ages.put(20);

JSONArray names = new JSONArray();
names.put("Mahesh");
names.put("Krishn");
names.put("Gopal");

JSONObject jsonObject = ages.toJSONObject(names);
System.out.println(jsonObject.toString(2)); 
Output
{
  "Krishn": 15,
  "Mahesh": 10,
  "Gopal": 20
} 

6. Reference

Class JSONArray
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us