英文:
Save each element from ArrayList into new line with Jackson
问题
我有一个POJO:
public class Game {
private String title;
private Set<String> genres;
private String size;
private List<String> screenshots;
}
我想将Game对象保存为JSON。
ObjectMapper
配置:
public static ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.enable(SerializationFeature.INDENT_OUTPUT);
将对象保存为JSON:
mapper.writeValue(new File(PATH), game)
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"
]
}
我该如何做到这一点?
英文:
I have a POJO:
public class Game {
private String title;
private Set<String> genres;
private String size;
private List<String> screenshots;
}
I want to save Game object to JSON.
ObjectMapper
configuration:
public static ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.enable(SerializationFeature.INDENT_OUTPUT);
Save object to JSON:
mapper.writeValue(new File(PATH), game)
And JSON looks like:
{
"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" ]
}
As you can see, all screenshots prints like one string, but I want the result like this:
{
"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",
]
}
How can I do it?
答案1
得分: 2
你可以使用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"
]
}
英文:
You can use writerWithDefaultPrettyPrinter of ObjectMapper like the follow code:
Game game = new Game();
ObjectMapper mapper = new ObjectMapper();
File file = new File("pretty-print.json");
mapper.writerWithDefaultPrettyPrinter().writeValue(file, game);
The output of your data is:
{
"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" ]
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论