简体   繁体   中英

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM