- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we add a JSONArray within JSONObject in Java?
A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.
Syntax
public JSONObject put(java.lang.String key, java.util.Collection<?> value) throws JSONException
Example
import org.json.*; public class AddJSONArrayTest { public static void main(String[] args) throws JSONException { JSONArray array = new JSONArray(); array.put("INDIA"); array.put("AUSTRALIA"); array.put("ENGLAND"); JSONObject obj = new JSONObject(); obj.put("COUNTRIES", array); System.out.println(obj); } }
Output
{"COUNTRIES":["INDIA","AUSTRALIA","ENGLAND"]}
- Related Articles
- How can we add a JSONArray to JSONObject in Java?
- How can we sort a JSONArray in Java?
- How can we sort a JSONObject in Java?
- How can we convert a JSONArray to String Array in Java?
- How can we add padding to a JTextField in Java?
- Can we declare a static variable within a method in java?
- How can we add/insert a JButton to JTable cell in Java?
- How can we nest a subquery within another subquery?
- How we can use Python Tuple within a Tuple?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?
- Can we add null elements to a Set in Java?
- How can we add new tabs to JTabbedPane from a JMenu in Java?
- Can we declare a try catch block within another try catch block in Java?
- How can we add different font style items to JList in Java?

Advertisements