Java – format JSON before writing to file

At present, I am using Jackson JSON processor to write preference data and files, mainly because I want advanced users to modify / back up these data Jackson is great because it's very easy to use and obviously performs well (see here), but the only problem I seem to encounter is when I run myobjectmapper When writevalue (myfile, myjsonobjectnode), it will write all data in one row in the objectnode What I want to do is format JSON in a more user - friendly format

For example, if I pass a simple JSON tree to it, it will write the following:

{"testArray":[1,2,3,{"testObject":true}],"anotherObject":{"A":"b","C":"d"},"string1":"i'm a string","int1": 5092348315}

I want it to appear in the file as:

{
    "testArray": [
        1,{
            "testObject": true
        }
    ],"anotherObject": {
        "A": "b","C": "d"
    },"string1": "i'm a string","int1": 5092348315
}

Does anyone know that I can do this in Jackson's way, or do I have to get the JSON string from Jackson and format it with another third-party lib?

Thank you in advance!

Solution

Try creating an object writer like this

ObjectWriter writer = mapper.defaultPrettyPrintingWriter();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>