Java 使用Jackson将ArrayList中的每个元素保存到新行中

Java 使用Jackson将ArrayList中的每个元素保存到新行中,java,jackson,Java,Jackson,我有一个POJO: public class Game { private String title; private Set<String> genres; private String size; private List<String> screenshots; } 将对象保存到JSON: mapper.writeValue(新文件(路径),游戏) JSON看起来像: { "title" : &quo

我有一个POJO:

public class Game {

    private String title;

    private Set<String> genres;

    private String size;

    private List<String> screenshots;
}
将对象保存到JSON:
mapper.writeValue(新文件(路径),游戏)

JSON看起来像:

{
  "title" : "Dead Cells",
  "genres" : [ "Action" ],
  "size" : "761M",
  "screenshots" : [ "https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx-Ygm9molYPAcMHhhZyQ0udDBd9S9-44v_VBdeA0rjSlQyJRpQg=w1440-h620-rw", "https://lh3.googleusercontent.com/mo0CZaV_aGflOPB8Tzo697l1WoZuoYUN9TiPMWq0zE29v_I99n1Qg185MfHrU-53nxAG=w1440-h620-rw", "https://lh3.googleusercontent.com/FEiHmVyoT1MU3rbAxSkE_aNDuXBuo3YHQOnqfMAfehS-d4k6CvxuyxpX6KKSbJp3Xv28=w1440-h620-rw", "https://lh3.googleusercontent.com/3Zg_EtwMpt-vWNBTdCNE7hP8M6qeDMq91HKfx70FSJ5tVAsPxHkTYWGiCwvHg5ucMykK=w1440-h620-rw", "https://lh3.googleusercontent.com/A9K6iPYty9IvkzeO_29ONdPAnFFs1BBzk6w-dAf1s5JgpZFMpxc5wpbz07fzovJWxlzO=w1440-h620-rw" ]
}
如您所见,所有屏幕截图都打印成一个字符串,但我希望得到如下结果:

{
  "title" : "Dead Cells",
  "genres" : [ "Action" ],
  "size" : "761M",
  "screenshots" : [ "https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx=w1440-h620-rw", 
                    "https://lh3.googleusercontent.com/I99n1Qg185MfHrU-53nxAG=w1440-h620-rw", 
                    "https://lh3.googleusercontent.com/d4k6CvxuyxpX6KKSbJp3Xv28=w1440-h620-rw", 
                    "https://lh3.googleusercontent.com/wvHg5ucMykK=w1440-h620-rw", 
                  ]
}

如何操作?

您可以使用ObjectMapper的writerWithDefaultPrettyPrinter,如下代码所示:

Game game = new Game();
ObjectMapper mapper = new ObjectMapper();
File file = new File("pretty-print.json");
mapper.writerWithDefaultPrettyPrinter().writeValue(file, game);
您的数据输出为:

{
  "title" : "Dead Cells",
  "genres" : [ "Action" ],
  "size" : "761M",
  "screenshots" : [ 
"https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx-Ygm9molYPAcMHhhZyQ0udDBd9S9-44v_VBdeA0rjSlQyJRpQg=w1440-h620-rw", 
"https://lh3.googleusercontent.com/mo0CZaV_aGflOPB8Tzo697l1WoZuoYUN9TiPMWq0zE29v_I99n1Qg185MfHrU-53nxAG=w1440-h620-rw", 
"https://lh3.googleusercontent.com/FEiHmVyoT1MU3rbAxSkE_aNDuXBuo3YHQOnqfMAfehS-d4k6CvxuyxpX6KKSbJp3Xv28=w1440-h620-rw", 
"https://lh3.googleusercontent.com/3Zg_EtwMpt-vWNBTdCNE7hP8M6qeDMq91HKfx70FSJ5tVAsPxHkTYWGiCwvHg5ucMykK=w1440-h620-rw", 
"https://lh3.googleusercontent.com/A9K6iPYty9IvkzeO_29ONdPAnFFs1BBzk6w-dAf1s5JgpZFMpxc5wpbz07fzovJWxlzO=w1440-h620-rw" ]
}

您需要删除第二个json的最后一个逗号,以使其正确无误。您会遇到什么问题
{
  "title" : "Dead Cells",
  "genres" : [ "Action" ],
  "size" : "761M",
  "screenshots" : [ 
"https://lh3.googleusercontent.com/af9mFH4XinZ7f6dx-Ygm9molYPAcMHhhZyQ0udDBd9S9-44v_VBdeA0rjSlQyJRpQg=w1440-h620-rw", 
"https://lh3.googleusercontent.com/mo0CZaV_aGflOPB8Tzo697l1WoZuoYUN9TiPMWq0zE29v_I99n1Qg185MfHrU-53nxAG=w1440-h620-rw", 
"https://lh3.googleusercontent.com/FEiHmVyoT1MU3rbAxSkE_aNDuXBuo3YHQOnqfMAfehS-d4k6CvxuyxpX6KKSbJp3Xv28=w1440-h620-rw", 
"https://lh3.googleusercontent.com/3Zg_EtwMpt-vWNBTdCNE7hP8M6qeDMq91HKfx70FSJ5tVAsPxHkTYWGiCwvHg5ucMykK=w1440-h620-rw", 
"https://lh3.googleusercontent.com/A9K6iPYty9IvkzeO_29ONdPAnFFs1BBzk6w-dAf1s5JgpZFMpxc5wpbz07fzovJWxlzO=w1440-h620-rw" ]
}