My code to create a new JSONObject and write to a file:
JSONObject obj = new JSONObject();
obj.put(\"name\", \"abcd\");
obj.put(\"age\", new Integer(100));
J
The method toJSONString()
is from json-simple
but I guess you are using org.json
.
org.json
have an alternative to toJSONString()
.
You can simply use: obj.toString(1)
.
The difference to the toString()
method is, that if you pass "1" as parameter org.json
automatically will format and beautify your JSON
.
So you don't have only one single compressed line of JSON
in your file.