org.json.JSONArray may be what you want.
String message;
JSONObject json = new JSONObject();
json.put("name", "student");
JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);
json.put("course", array);
message = json.toString();
// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}
Answer from srain on Stack OverflowGeeksforGeeks
geeksforgeeks.org › java › working-with-json-data-in-java
Working with JSON Data in Java - GeeksforGeeks
December 23, 2025 - It is easy to read, write, and ... in key–value pairs and supports arrays and nested objects. Example: Storing student information in JSON format....
DigitalOcean
digitalocean.com › community › tutorials › java-json-example
Java JSON Example | DigitalOcean
August 4, 2022 - Streaming API - It’s similar to StaX Parser and good for large objects where you don’t want to keep whole object in memory. ... javax.json.JsonReader: We can use this to read JSON object or an array to JsonObject.
Videos
04:28
Java - Mapping objects to JSON - YouTube
14:44
Create Complex Json in Java | Json Object | Json Array In Java ...
A quick tutorial on using JSON-simple parsing JSON ...
02:44
How to convert JSON to Java objects using Gson - YouTube
18:29
JSON in Java - Writing to Files - YouTube
06:42
Java Gets a JSON API - Inside Java Newscast #95 - YouTube
TutorialsPoint
tutorialspoint.com › json › json_java_example.htm
JSON with Java
The following example makes use of JSONObject and JSONArray where JSONObject is a java.util.Map and JSONArray is a java.util.List, so you can access them with standard operations of Map or List. import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.json.simple.parser.JSONParser; class JsonDecodeDemo { public static void main(String[] args) { JSONParser parser = new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.pr
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObject.html
JsonObject (Java(TM) EE 7 Specification APIs)
JsonObject class represents an immutable JSON object value (an unordered collection of zero or more name/value pairs). It also provides unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using JsonReader.readObject(). For example:
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
For example, given a JSONObject initialized with this document: ... "/a/b" Then this method will return the String "c". A JSONPointerException may be thrown from code called by this method. ... Queries and returns a value from this object using jsonPointer, or returns null if the query fails ...
Top answer 1 of 6
266
org.json.JSONArray may be what you want.
String message;
JSONObject json = new JSONObject();
json.put("name", "student");
JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);
json.put("course", array);
message = json.toString();
// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}
2 of 6
13
In contrast to what the accepted answer proposes, the documentation says that for JSONArray() you must use put(value) no add(value).
https://developer.android.com/reference/org/json/JSONArray.html#put(java.lang.Object)
(Android API 19-27. Kotlin 1.2.50)
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-java › lessons › creating-and-writing-json-data-with-java-using-jackson
Creating and Writing JSON Data with Java
Serialize to JSON: Use the Jackson library's ObjectMapper class to serialize these objects into a JSON string, ready for storage or transmission. These steps form the foundation of translating structured class-based data into a JSON format, seamlessly bridging Java applications with JSON data handling.
Medium
medium.com › @Mohd_Aamir_17 › mastering-json-in-java-a-comprehensive-guide-to-handling-json-objects-arrays-and-nodes-with-df57bf0ebff1
Mastering JSON in Java: A Comprehensive Guide to Handling JSON Objects, Arrays, and Nodes with Jackson | by Mohd Aamir | Medium
November 4, 2024 - You are dealing with dynamic or nested structures, where some fields may be arrays, while others are objects or primitives. You need a flexible approach to handle JSON without strict adherence to a particular structure. In essence, ArrayNode is suitable for structured JSON arrays, while JsonNode is ideal for complex, dynamic JSON processing. One of Jackson’s strongest features is its ability to easily convert JSON data into Java objects, and vice versa.
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONObject.html
JSONObject
This is the most commonly used JSONObject constructor. ... Get the value object associated with a key. ... The object associated with the key. ... A string which is the value. ... Get an optional string associated with a key. It returns the defaultValue if there is no such key. ... A string which is the value. ... The integer value. ... java.lang.RuntimeException - if the key is not found or if the value cannot be converted to an integer.
Javatpoint
javatpoint.com › java-json-example
Java JSON Example
Java JSON example for beginners and professionals with examples of JSON with java, install json.simple, java json encode, java json encode using map, java json array encode, java json array encode using List, java json decode. Learn Java JSON example with array, object, schema, encode, decode, file, date etc.
GitHub
github.com › stleary › JSON-java
GitHub - stleary/JSON-java: A reference implementation of a JSON package in Java. · GitHub
The unit tests can be executed ... ... import org.json.JSONObject; public class Test { public static void main(String args[]){ JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }"); System.out.println(jo); } }...
Starred by 4.7K users
Forked by 2.6K users
Languages Java
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › com › oracle › json › JsonObject.html
JsonObject (JSON Documentation)
The map object's iteration ordering is based on the order in which name/value pairs are added to the corresponding builder or the order in which name/value pairs appear in the corresponding stream. ... clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values ... Returns the array value to which the specified name is mapped. This is a convenience method for (JsonArray)get(name) to get the value. ... the array value to which the specified name is mapped, or null if this object contains no mapping for the name ... java.lang.ClassCastException - if the value to which the specified name is mapped is not assignable to JsonArray type
Medium
medium.com › @benpourian › how-to-create-a-java-object-from-a-json-object-c47c08b030c5
How to create a Java Object from a JSON object | by Benjamin Pourian | Medium
October 25, 2018 - This will be a very brief guide to creating a Java object from a JSON object using the popular gson` google library. I will first demonstrate a simple JSON → POJO example then follow that with another example where I will be using annotations provided by gson to instantiate a new object by ...
Oracle
docs.oracle.com › javaee › 7 › tutorial › jsonp001.htm
19.1 Introduction to JSON - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications. The following sections provide an introduction to JSON syntax, an overview of JSON uses, and a description of the most common approaches to generate and parse JSON. JSON defines only two data structures: objects ...
Coderanch
coderanch.com › t › 706235 › java › Identifying-JSONObject-JSONArray
Identifying JSONObject or JSONArray (Java in General forum at Coderanch)
Here are partial examples: This is the normal string that works: This is the response that causes the parser exception: ... Sam Ritter wrote:... is there a way to determine which sb is before running the parser? The easiest way is probably to use the parser to determine if JSON element is what you are expecting - as you have found, it will throw an exception if it is the wrong type of element.