Org.Json - HTTP Class



A JSONArray class object is an ordered sequence of values. It provides methods to access values by index and to put values. Following types are supported −

  • Boolean

  • JSONArray

  • JSONObject

  • Number

  • String

  • JSONObject.NULL object

Example - JsonArray of Strings

JsonDemo.java

package com.tutorialspoint; import org.json.JSONArray; public class JsonDemo { public static void main(String[] args) { JSONArray list = new JSONArray(); list.put("Apple"); list.put("Banana"); list.put("Orange"); list.put("Mango"); list.put("Guava"); System.out.println("JSONArray: "); System.out.println(list); } }

Output

JSONArray: 
["Apple","Banana","Orange","Mango","Guava"]

Example - JsonArray of Integers

JsonDemo.java

package com.tutorialspoint; import org.json.JSONArray; public class JsonDemo { public static void main(String[] args) { JSONArray list = new JSONArray(); list.put(Integer.valueOf(100)); list.put(Integer.valueOf(200)); list.put(Integer.valueOf(300)); list.put(Integer.valueOf(400)); list.put(Integer.valueOf(500)); System.out.println("JSONArray: "); System.out.println(list); } }

Output

JSONArray: 
[100,200,300,400,500]

Example - JsonArray of Mixed Types

JsonDemo.java

package com.tutorialspoint; import org.json.JSONArray; import org.json.JSONObject; public class JsonDemo { public static void main(String[] args) { JSONArray list = new JSONArray(); list.put("foo"); list.put(Integer.valueOf(100)); list.put(Double.valueOf(1000.21)); list.put(Boolean.TRUE); list.put(JSONObject.NULL); System.out.println("JSONArray: "); System.out.println(list); } }

Output

JSONArray: 
["foo",100,1000.21,true,null]
Advertisements