Как записать массив Json в файл с помощью jacksonJAVA

Программисты JAVA общаются здесь
Anonymous
Как записать массив Json в файл с помощью jackson

Сообщение Anonymous »

Я создал файл Json, в котором хотел написать объект Java как элемент массива. Я использую Джексон.
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);
}

Это не то, что мне нужно. Он создает данные типа
{
"storyTitle" : "ttt",
"storyBody" : "tttt",
"storyAuthor" : "tttt"
}
{
"storyTitle" : "a",
"storyBody" : "a",
"storyAuthor" : "a"
}

Мне просто нужно создать массив Json, куда я добавляю Java-объект, данные должны быть такими
[{
"storyTitle" : "ttt",
"storyBody" : "tttt",
"storyAuthor" : "tttt"
}
,{
"storyTitle" : "a",
"storyBody" : "a",
"storyAuthor" : "a"
}]


Подробнее здесь: https://stackoverflow.com/questions/346 ... ng-jackson

Вернуться в «JAVA»