Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Following is the proper way to initialize an <code>JSONArray</code></p> <pre><code>public class TestJSON { public static void main(String[] args) { JSONArray template = new JSONArray("[ {\"name\": \"red\", \"first\": true, \"url\": \"#Red\"}," + " {\"name\": \"green\", \"link\": true, \"url\": \"#Green\"}," + "{\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"} ]"); System.out.println(template.toString()); } } </code></pre> <p>Following is the output:</p> <pre><code>[ { "name": "red", "first": true, "url": "#Red" }, { "link": true, "name": "green", "url": "#Green" }, { "link": true, "name": "blue", "url": "#Blue" } ] </code></pre> <p>EDIT1:</p> <p>You can use the following code to create your full JSON object..</p> <pre><code> public static void main(String[] args) { JSONArray template = new JSONArray( "[ {\"name\": \"red\", \"first\": true, \"url\": \"#Red\"}," + " {\"name\": \"green\", \"link\": true, \"url\": \"#Green\"}," + "{\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"} ]"); JSONObject object = new JSONObject(); object.put("header", "Colors"); object.put("empty", false); object.put("items", template); System.out.println(object.toString()); } </code></pre> <p>Following is the output:</p> <pre><code>{ "items": [ { "name": "red", "first": true, "url": "#Red" }, { "link": true, "name": "green", "url": "#Green" }, { "link": true, "name": "blue", "url": "#Blue" } ], "empty": false, "header": "Colors" } </code></pre> <p>EDIT2:</p> <p>Following code can be used to generate the full JSON object without using string containing JSON data:</p> <pre><code> public static void main(String[] args) { JSONArray template = new JSONArray(); JSONObject obj = new JSONObject(); obj.put("name", "red"); obj.put("first", true); obj.put("url", "#Red"); template.put(obj); JSONObject obj1 = new JSONObject(); obj1.put("name", "green"); obj1.put("link", true); obj1.put("url", "#Green"); template.put(obj1); JSONObject obj2 = new JSONObject(); obj2.put("name", "blue"); obj2.put("link", true); obj2.put("url", "#Blue"); template.put(obj2); JSONObject object = new JSONObject(); object.put("header", "Colors"); object.put("empty", false); object.put("items", template); System.out.println(object.toString()); } </code></pre> <p>Following is the output of this program:</p> <pre><code>{ "items": [ { "name": "red", "first": true, "url": "#Red" }, { "link": true, "name": "green", "url": "#Green" }, { "link": true, "name": "blue", "url": "#Blue" } ], "empty": false, "header": "Colors" } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload