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 Overfloworg.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"}
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)
Videos
TL;DR: Looking for a tool that can generate example-json from POJOs
We use an event-driven architecture and dispatch the event-payloads as json-strings. We also have to document the structure of these payloads and currently we do this by typing json-examples manually.
The payloads are basically just POJOs with a constructor, getters and setters.
I'm looking for a tool to streamline the documentation-process! Is there a software that can generate example json from Java classes?
I'm really thankful for any recommendation!
below is a copy of the string i want to edit and send but when i try to use a varible in the string i get a error ( string litral ) i think its because i not formating the escape char correcly but im lost can any show me how to use escape chars with in a string
httpPostRequests testing = new httpPostRequests("{\"jsonrpc\":\"2.0\",\"method\":\"getaccount\",\"params\":{\""\account\":1920},\"id\":123}","http://localhost:4003");