An example of an interchange of JSON strings and Java objects in Java

Source: Internet
Author: User

Http://www.jb51.net/article/90914.htm

In the development process, often need to exchange data with other systems, the format of data exchange is XML, JSON, etc., JSON as a lightweight data format than XML efficiency, XML needs a lot of tags, which undoubtedly occupy the network traffic, JSON is doing well in this respect, Let's look at the JSON format below,

JSON can be in two formats, one in object format and the other as an array object.

{"Name": "JSON", "Address": "Xicheng District, Beijing", "age": 25}//json Object-formatted string

[{"Name": "JSON", "Address": "Xicheng District, Beijing", "age": 25}]//data Object Format

From the above two formats can be seen in the object format and the format of the array object is the only difference is the object format based on the addition of [], and then to see the specific structure, you can see the form of key-value pairs, the middle of the English state of the comma (,) separated.

This format is also popular when transmitting data at the front and back ends, and the backend returns a JSON-formatted string, and the foreground uses the Json.parse () method in JS to parse the JSON string into a JSON object and then traverse it for use by the front end.

Let's go to the bottom and introduce the interaction between JSON and Java objects in Java.

To implement the interaction between JSON and Java objects, you need a third-party jar package, which uses the Json-lib jar package: https://sourceforge.net/projects/json-lib/, Json-lib need Commons-beanutils-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 Five packs of support, you can download from the Internet, no longer posted here.

Json-lib provides several classes that can accomplish this function, for example, Jsonobject, Jsonarray. From the name of the class you can see that the Jsonobject transformation should be the object format, while the Jsonarray conversion should be the array object (i.e., with [] form).

One, Java common Object and JSON string of the mutual transfer

Java Object--"string"

Java generic object refers to a Java Bean in Java, that is, an entity class, such as,

An example of an interchange of JSON strings and Java objects in Java (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.