MOHAMMED SALEH
MOHAMMED SALEH

Reputation: 143

How to add a new line between each JSONObject in a JSONArray, using json.simple and java

I am trying to write to a json file using a JSONArray object from the json.simple library. The problem that I am having is that the format of the file comes out like this:

[{"artist_name":"test1","year":0,"album_name":null,"song_name":"test1"},{"artist_name":"test1","year":0,"album_name":null,"song_name":"test1"}]

when I would like the format to come out like this:

[{"artist_name":"test1",
"year":0,
"album_name":null,
"song_name":"test1"},

{"artist_name":"test1",
"year":0,
"album_name":null,
"song_name":"test1"}]

this is what my code looks like right now:

        FileWriter filewriter = new FileWriter("filesystem.json");

        JSONObject newo = new JSONObject();
        newo.put("song_name", "test1");
        newo.put("artist_name", "test1");
        newo.put("album_name", null);
        newo.put("year", 0);

        arr.add(newo);
        filewriter.write(arr.toJSONString());
        filewriter.flush();
        filewriter.close();

Upvotes: 0

Views: 1586

Answers (1)

KeitL
KeitL

Reputation: 208

You don't need to add something to your code. Json always will looks like this, when you retrieve it from somewhere. If you want to see it in way you describe try google json viewers it could help.

Upvotes: 2

Related Questions