Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have data in a Map object and I want to print it in a json format. I tried using DefaultPrettyPrinter

mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject);

but the format is not what I expected. I am getting output like this:

{
  "arrVals" : ["value-1","value-2"]
}

I want output like this:

{
  "arrVals" : [
    "value-1",
    "value-2"
  ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
443 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need indentation before Array Values. You can use indentArraysWith method to set the Lf2SpacesIdenter object which will basically add a line feed followed by 2 spaces. This might solve your problem.

DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new Lf2SpacesIndenter());
mapper.writer(pp).writeValue(filePath, mapObject);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...