Example of conversion between JSON and objects and collections

Conversion of JSON string and Java Object [JSON lib]

During the development process, it is often necessary to exchange data with other systems. The formats of data exchange include XML and JSON. JSON, as a lightweight data format, is more efficient than XML. XML requires a lot of tags, which undoubtedly occupies the network traffic. JSON does a good job in this regard. Let's take a look at the format of JSON first,

JSON can have two formats, one is object format, and the other is array object,

From the above two formats, we can see that the only difference between the object format and the array object format is the addition of [] on the basis of the object format. Looking at the specific structure, we can see that they all appear in the form of key value pairs, separated by commas (,) in English.

This format is also very popular when transmitting data between the front end and the back end. The back end returns a string in JSON format, and the front end uses JSON in JS The parse () method parses the JSON string into a JSON object, and then traverses it for use by the front end.

Let's get to the point and introduce the mutual transformation between JSON and Java objects in Java.

To realize the mutual conversion between JSON and Java objects, you need to use the third-party jar package. Here, the jar package JSON lib is used. The download address is: https://sourceforge.net/projects/json-lib/ , JSON lib requires commons-bean utils-1.8 0.jar、commons-collections-3.2. 1.jar、commons-lang-2.5. jar、commons-logging-1.1. 1.jar、ezmorph-1.0. 6. Jar supports five packages, which can be downloaded from the Internet. The download address is no longer posted here.

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