如何使用jackson将Json数组写入文件

时间:2023-02-07 15:25:02

I created a Json file where i wanted to write write java object as Array element. Im using jackson.

我创建了一个Json文件,我想写java对象作为Array元素。我用杰克逊。

    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 exactly want .it creates data like

这不是我想要的。它创建数据

{
  "storyTitle" : "ttt",
  "storyBody" : "tttt",
  "storyAuthor" : "tttt"
}
{
  "storyTitle" : "a",
  "storyBody" : "a",
  "storyAuthor" : "a"
}

I just need to create a Array of Json where i add java object, data should be like this

我只需要创建一个Json数组,我在其中添加java对象,数据应该是这样的

[{
  "storyTitle" : "ttt",
  "storyBody" : "tttt",
  "storyAuthor" : "tttt"
}
,{
  "storyTitle" : "a",
  "storyBody" : "a",
  "storyAuthor" : "a"
}]

2 个解决方案

#1


5  

Jackson provide inbuilt methods for writing JSON data to JSON file. you can use these sort of code line for this

Jackson提供了用于将JSON数据写入JSON文件的内置方法。你可以使用这些代码行

ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("D:/cp/dataTwo.json"), jsonDataObject);
//new file(path of your file) 

and jsonDataObject is your actual object(i.e object or array) which you want to write in file.

和jsonDataObject是您要在文件中写入的实际对象(即对象或数组)。

#2


0  

It can be done by using arrays:

它可以通过使用数组来完成:

    ObjectMapper objectMapper = new ObjectMapper();

    Student student = new Student();

    student.setActive(false);
    student.setFirstName("Kir");
    student.setId(123);
    student.setLastName("Ch");

    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

    try {
        List<Student> listOfStudents = new ArrayList<>();
        listOfStudents.add(student);
        listOfStudents.add(student);
        objectMapper.writeValue(new File("d:/temp/output.json"), listOfStudents);
    } catch (IOException e) {
        e.printStackTrace();
    }

The result will be like this:

结果将是这样的:

[ {
  "id" : 123,
  "firstName" : "Kir",
  "lastName" : "Ch",
  "active" : false,
  "address" : null,
  "languages" : null
}, {
  "id" : 123,
  "firstName" : "Kir",
  "lastName" : "Ch",
  "active" : false,
  "address" : null,
  "languages" : null
} ]

#1


5  

Jackson provide inbuilt methods for writing JSON data to JSON file. you can use these sort of code line for this

Jackson提供了用于将JSON数据写入JSON文件的内置方法。你可以使用这些代码行

ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("D:/cp/dataTwo.json"), jsonDataObject);
//new file(path of your file) 

and jsonDataObject is your actual object(i.e object or array) which you want to write in file.

和jsonDataObject是您要在文件中写入的实际对象(即对象或数组)。

#2


0  

It can be done by using arrays:

它可以通过使用数组来完成:

    ObjectMapper objectMapper = new ObjectMapper();

    Student student = new Student();

    student.setActive(false);
    student.setFirstName("Kir");
    student.setId(123);
    student.setLastName("Ch");

    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

    try {
        List<Student> listOfStudents = new ArrayList<>();
        listOfStudents.add(student);
        listOfStudents.add(student);
        objectMapper.writeValue(new File("d:/temp/output.json"), listOfStudents);
    } catch (IOException e) {
        e.printStackTrace();
    }

The result will be like this:

结果将是这样的:

[ {
  "id" : 123,
  "firstName" : "Kir",
  "lastName" : "Ch",
  "active" : false,
  "address" : null,
  "languages" : null
}, {
  "id" : 123,
  "firstName" : "Kir",
  "lastName" : "Ch",
  "active" : false,
  "address" : null,
  "languages" : null
} ]