The Jackson framework of Java can easily convert JSON

Jackson can easily convert Java objects into JSON objects and XML documents. He can also convert JSON and XML into Java objects.

Compared with the JSON lib framework, Jackson relies on fewer jar packages, which is easy to use and has higher performance. Moreover, Jackson community is relatively active and updated quickly.

1、 Preparatory work

1. Download dependent library jar package

Jackson's jar all download address: http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar

Then import the jar package into the project to start working

Official example: http://wiki.fasterxml.com/JacksonInFiveMinutes

Because the following program is run with JUnit test cases, you have to add JUnit's jar package. The version is junit-4.2 eight

If you need to convert XML, you also need stax2 API jar

2. The basic code of the test class is as follows

3. Required javaentity

Birthday

2、 Convert Java objects to JSON

1. Convert JavaBean (entity / model) to JSON

The results after operation are as follows:

The above uses the writeobject method of jsongenerator and the writevalue method of objectmapper to complete the conversion of Java objects. The parameters passed and the construction methods are different; The creation of the jsongenerator depends on the objectmapper object. That is, if you want to use jsongenerator to convert JSON, you must create an objectmapper. However, if you use objectmapper to convert JSON, you do not need jsongenerator.

The writevalue method of objectmapper can convert a Java object into JSON. Parameter 1 of this method needs to provide an output stream. After conversion, the converted content can be output through this stream. Or provide a file to write the converted content to the file. Of course, this parameter can also receive a jsongenerator, and then output the converted information through jsongenerator. The second parameter is the Java object to be converted. If the method with three parameters is used, it is a config. This config can provide some conversion rules, such as filtering or conversion through some properties of the specified Java object.

2. Convert the map collection to a JSON string

The results after conversion are as follows:

3. Convert list collection to JSON

The results are as follows:

There are many [] brackets outside; Similarly, array can also be converted. The converted JSON is the same as the above result. There is no conversion here. ~~

4. Let's take a look at some types provided by Jackson and use these types to complete JSON conversion; If you use these types to convert JSON, you can complete the JSON conversion of complex Java types even without JavaBean (entity). The following uses these types to build a complex Java object and complete JSON conversion.

After running, the results are as follows:

What about? The constructed JSON string is consistent with the output result. The key is to understand how to use the method provided by jsongenerator to complete the construction of an object.

3、 Convert JSON to Java objects

1. Convert JSON strings to JavaBean objects

Very simply, the readvalue method of the objectmapper object is used. This method needs to provide two parameters. The first parameter is the parsed JSON string, and the second parameter is the Java object to be parsed and the type of Java object. Of course, there are other similar signature methods. If you are interested, you can try them one by one. Of course, the methods used are similar to those currently used. After running, the results are as follows:

2. Convert JSON string to list < Map > collection

I tried to convert the above JSON into a list, and then store the accountbean in the list, but the result failed. However, map collections are not supported. Because you turned into a list Class, but I don't know what type list stores. The map type has to be silent. Because all objects can be converted into map combination, the results after running are as follows:

3. The JSON string is converted to an array. Because the above generic conversion does not recognize the object type in the collection. All the object arrays used here can solve this problem. But it is no longer a collection, but an array. Of course, this is not important. You can use arrays Aslist can be converted into a list.

Results after operation:

4. Convert JSON string to map set

The results after operation are as follows:

4、 Jackson's support for XML

Jackson can also complete the conversion from Java objects to XML. The result of the conversion is more intuitive than JSON lib, but it depends on stax2 API Jar this jar package.

Run the above method, and the results are as follows:

As a result, the problem that the root node is unknow has not been solved. Because the root node has not been converted, it is impossible to parse XML to Java objects.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>