Detailed explanation of the exchange example between JSON string and Java object in Java

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,

{"name": "JSON", "address": "Xicheng District, Beijing", "age": 25} / / string in JSON object format

[{"name": "JSON", "age": 25}] / / data object format

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.

JSON lib provides several classes to complete this function, such as jsonobject and jsonarray. From the name of the class, we can see that jsonobject should convert in object format, while jsonarray should convert in array object (that is, with [] form).

1、 Conversion of Java ordinary object and JSON string

Java object -- string

Java common object refers to a java bean in Java, that is, an entity class, such as,

The above is my ordinary Java entity class. See how JSON lib converts it into string form,

I defined a student's entity class, and then converted it into JSON strings using jsonobject and jsonarray respectively. See the printed results below,

Strjson: {"address": "Xicheng District, Beijing", "age": "23", "name": "JSON"} strarray: [{"address": "Xicheng District, Beijing", "name": "JSON"}]

From the results, we can see that both methods can convert Java objects into JSON strings, but the converted structures are different.

JSON string -- Java object

The above explains how to convert Java objects into JSON strings. Let's see how to convert JSON string format into Java objects,

First, you need to define two strings in different formats. You need to use \ to escape double quotes,

The print result is:

Stu: student [name = JSON, age = 24, address = Xicheng District, Beijing] stu2: student [name = JSON, address = Xicheng District, Beijing]

As can be seen from the above code, using jsonobject can easily convert a string in JSON format into a Java object, but using jsonarray is not so easy because it has the "[]" symbol. Therefore, after obtaining the jsonarray object, we take the first element, that is, the deformation of a student we need, and then use jsonobject to easily obtain it.

2、 Conversion of list and JSON strings

List -- JSON string

I've left out the method of using jsonobject. Let's look at the results before the annotation,

Exception in thread "main" net. sf. json. JSONException: 'object' is an array. Use jsonarray instead told me that there was an exception. By looking at the source code, I found that the parameter type will be judged first when using the fromobject method. Here I tell us that the passed in parameter is an array type because of the ArrayList used. Let's look at the results after annotation,

Listarray: [{"address": "Haidian District, Beijing", "name": "JSON"}] the result is normal.

JSON string -- List

From the above example, we can see that the list object can only be converted into the format of array object. Let's look at the conversion from the following string to list,

Print results,

Student [name = JSON, address = Xicheng District, Beijing]

Student [name = JSON, address = Xicheng District, Beijing]

Because the format of the string is in the format of "[]", jsonarray is selected here. It has toArray and tolist methods. The former is converted into an array in Java or a list in Java. Because there is an entity class for correspondence, the generic type (student. Class) is specified during use, so the converted object can be obtained.

3、 Conversion of map and JSON strings

Map -- JSON string

Print results,

MapObject {"first": {"address": "Shanghai, China", "name": "JSON"}} maparray: [{"first": {"address": "Shanghai, China", "name": "JSON"}}]

Two forms are printed on it.

JSON string -- map

The JSON string cannot be directly converted into a map object. To obtain the value corresponding to the key in the map, you need to use other methods,

Print results,

Student [name = JSON, age = 23, address = Shanghai, China]

The following is the code of mybean,

Using the tobean () method, three parameters are passed in. The first is the jsonobject object and the second is mybean Class, the third is a map object. Through mybean, you can know that there should be a first attribute in this class, and its type is student, which should correspond to the key and value types in the map, that is, the first corresponds to the key and the first type corresponds to the value type.

The above is a detailed explanation of the conversion examples between JSON strings and Java objects introduced by Xiaobian. I hope it will be helpful to you!

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