I created a Json file where I wanted to write a java write object as an Array element. I use jackson.
try{ String json; String phyPath = request.getSession().getServletContext().getRealPath("/"); String filepath = phyPath + "resources/" + "data.json"; File file = new File(filepath); if (!file.exists()) { System.out.println("pai nai"); file.createNewFile(); } json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(story); Files.write(new File(filepath).toPath(), Arrays.asList(json), StandardOpenOption.APPEND); }
This is not what I definitely want .it creates data like
{ "storyTitle" : "ttt", "storyBody" : "tttt", "storyAuthor" : "tttt" } { "storyTitle" : "a", "storyBody" : "a", "storyAuthor" : "a" }
I just need to create a Json array, where I add a java object, the data should look like
[{ "storyTitle" : "ttt", "storyBody" : "tttt", "storyAuthor" : "tttt" } ,{ "storyTitle" : "a", "storyBody" : "a", "storyAuthor" : "a" }]
source share