Java JSONArray Example
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>
JSONArray
class.
Contents
1. Instantiate JSONArray
TheJSONArray
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)
JSONArray
using Collection
.
List<String> list = List.of("Mahesh", "Krishna", "Brahma"); JSONArray jsonArray = new JSONArray(list); System.out.println(jsonArray.toString());
["Mahesh","Krishna","Brahma"]
JSONArray
using String array.
String[] ar = {"Mahesh", "Krishna", "Brahma"}; JSONArray jsonArray = new JSONArray(ar); System.out.println(jsonArray.toString());
["Mahesh","Krishna","Brahma"]
2. Putting Values
To put values inJSONArray
, we can use following methods.
a. Appends
Boolean
value.
JSONArray put(boolean value)
JSONArray put(int index, boolean value)
JSONArray jsonArray = new JSONArray(); jsonArray.put(true); jsonArray.put(false); jsonArray.put(true); System.out.println(jsonArray.toString());
[true,false,true]
int
value.
JSONArray put(int value)
double
and float
values.
Find the method to put or replace value.
JSONArray put(int index, int value)
JSONArray jsonArray = new JSONArray(); jsonArray.put(10); jsonArray.put(15); jsonArray.put(20); System.out.println(jsonArray.toString());
[10,15,20]
Collection
.
JSONArray put(Collection<?> value)
Collection
for the given index.
JSONArray put(int index, Collection<?> value)
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());
[["Varanasi","Prayagraj","Ayodhya"]]
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)
Map
for given index.
JSONArray put(int index, Map<?,?> value)
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));
[ { "103": "Anand", "101": "Mohit", "102": "Suresh" }, { "103": 30, "101": 25, "102": 20 } ]
3. Getting Values
TheJSONArray
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)
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()
Ex.
JSONArray jsonArray = new JSONArray(); jsonArray.put("Mahesh"); jsonArray.put("Krishn"); jsonArray.put("Gopal"); String output = jsonArray.toString(); System.out.println(output);
["Mahesh","Krishn","Gopal"]
String toString(int indentFactor)
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 thetoJSONObject
method declaration.
JSONObject toJSONObject(JSONArray names)
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));
{ "Krishn": 15, "Mahesh": 10, "Gopal": 20 }