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

huangapple go评论75阅读模式
英文:

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&lt;String&gt; genres;

    private String size;

    private List&lt;String&gt; screenshots;
}

I want to save Game object to JSON.
ObjectMapperconfiguration:

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:

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

As you can see, all screenshots prints like one string, but I want the result like this:

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

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(&quot;pretty-print.json&quot;);
mapper.writerWithDefaultPrettyPrinter().writeValue(file, game);

The output of your data is:

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

</details>



huangapple
  • 本文由 发表于 2020年8月14日 04:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63403065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定