json - How to write a JSONObject to a file, which has JSONArray inside it, in Java? -
i have json file, need read, edit , write out again.
reading works fine, struggle write part of json array in data.
i use json.simple library work json in java.
the file looks this:
{ "maxusers":100, "maxtextlength":2000, "maxfilesize":2000, "services": [ { "servicename":"Яндекc", "classname":"yandexconnector.class", "isenabled":true }, { "servicename":"google", "classname":"googleconnector.class", "isenabled":false } ] }
when try write json-data (variable obj
) file, services
array broken. writing 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<service> servicesarray = this.getservices(); for(int i=0; i< servicesarray.size(); i++) { servicesjson.add(servicesarray.get(i)); } obj.put("services", servicesjson); filewriter file = new filewriter(filename); obj.writejsonstring(file); file.flush(); file.close();
this outputs:
{ "services": [ translator.settings.service@121c5df, translator.settings.service@45f4ae ], "maxtextlength":2000, "maxusers":100, "maxfilesize":2000 }
how can write json data correctly file, if have in jsonarray services
?
the code, read json data file (that works fine):
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")); // list of services jsonarray serv = (jsonarray) jsonobject.get("services"); (int = 0; < 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); }
the editing part not yet written, call writing part directly after reading.
have @ examples of json-simple
.
it says here need put objects 1 one array, using primitive
, string
values. may use collections
map
contain string
or primitive
values.
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);
so, adding services
not allowed , won't work. should add tomap
method in convert map
, frommap
convert back.
like (in services.java
):
public map tomap() { hashmap<string, string> serviceasmap = new hashmap<>(); servicesasmap.put("servicename", servicename); servicesasmap.put("classname", this.class.getname() + ".class"); servicesasmap.put("isenabled", isenabled); // ... continue values return servicesasmap; }
then can use map
populate jsonarray
this:
jsonarray servicesjson = new jsonarray(); arraylist<service> servicesarray = this.getservices(); for(int i=0; i< servicesarray.size(); i++) { servicesjson.add(servicesarray.get(i).tomap()); // use tomap method here. } obj.put("services", servicesjson);
Comments
Post a Comment