Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a JSONObject to a file, which has JSONArray inside it, in Java?
    primarykey
    data
    text
    <p>I have JSON file, that I need to read, edit and write out again.</p> <p>Reading works fine, I struggle with the write part of the JSON Array in my data.</p> <p>I use <a href="https://github.com/fangyidong/json-simple" rel="nofollow">JSON.simple</a> library to work with JSON in Java.</p> <p>The file looks like this:</p> <pre><code>{ "maxUsers":100, "maxTextLength":2000, "maxFileSize":2000, "services": [ { "serviceName":"Яндекc", "className":"YandexConnector.class", "isEnabled":true }, { "serviceName":"Google", "className":"GoogleConnector.class", "isEnabled":false } ] } </code></pre> <p>When I try to write JSON-data (variable <code>obj</code>) to file, the <code>services</code> array is broken. My writing code:</p> <pre><code>JSONObject obj = new JSONObject(); obj.put("maxUsers", this.getMaxUsers()); obj.put("maxTextLength", this.getMaxTextLength()); obj.put("maxFileSize", this.getMaxFileSize()); 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)); } obj.put("services", servicesJSON); FileWriter file = new FileWriter(filename); obj.writeJSONString(file); file.flush(); file.close(); </code></pre> <p>This outputs:</p> <pre><code>{ "services": [ translator.settings.Service@121c5df, translator.settings.Service@45f4ae ], "maxTextLength":2000, "maxUsers":100, "maxFileSize":2000 } </code></pre> <p>How can I write the JSON data correctly to a file, if I have it in a JSONArray like <code>services</code> ? </p> <hr> <p>The code, where I read the JSON data from the file (that works fine):</p> <pre><code>JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(filename)); JSONObject jsonObject = (JSONObject) obj; setMaxUsers((Long) jsonObject.get("maxUsers")); setMaxTextLength((Long) jsonObject.get("maxTextLength")); setMaxFileSize((Long) jsonObject.get("maxFileSize")); // get all list of services JSONArray serv = (JSONArray) jsonObject.get("services"); for (int i = 0; i &lt; serv.size(); i++) { JSONObject service = (JSONObject) serv.get(i); Service servec = new Service(); servec.setServiceName((String) service.get("serviceName")); servec.setClassName((String) service.get("className")); servec.setIsEnabled((Boolean) service.get("isEnabled")); services.add(i, servec); } </code></pre> <p>The editing part is not yet written, so I call the writing part directly after the reading.</p> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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