Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a look at the <a href="http://code.google.com/p/json-simple/wiki/EncodingExamples" rel="noreferrer">examples</a> of <code>JSON-simple</code>.</p> <p>It says here that you need to put the Objects one by one into the Array, using only <code>primitive</code> and <code>String</code> values. You may use <code>Collections</code> like <code>Map</code> that by themselves only contain <code>String</code> or <code>primitive</code> values.</p> <pre><code> JSONArray list = new JSONArray(); list.add("foo"); list.add(new Integer(100)); list.add(new Double(1000.21)); list.add(new Boolean(true)); list.add(null); StringWriter out = new StringWriter(); list.writeJSONString(out); </code></pre> <p>So, adding your <code>Services</code> is not allowed and won't work. You should add a <code>toMap</code> method in it where you convert it to a <code>Map</code> and <code>fromMap</code> to convert it back.</p> <p>Like this (in <code>Services.java</code>):</p> <pre><code> public Map toMap() { HashMap&lt;String, String&gt; serviceAsMap = new HashMap&lt;&gt;(); servicesAsMap.put("serviceName", serviceName); servicesAsMap.put("className", this.class.getName() + ".class"); servicesAsMap.put("isEnabled", isEnabled); // ... continue for all values return servicesAsMap; } </code></pre> <p>then you can use that <code>Map</code> to populate your <code>JSONArray</code> like this:</p> <pre><code> JSONArray servicesJSON = new JSONArray(); ArrayList&lt;Service&gt; servicesArray = this.getServices(); for(int i=0; i&lt; servicesArray.size(); i++) { servicesJSON.add(servicesArray.get(i).toMap()); // use the toMap method here. } obj.put("services", servicesJSON); </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