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

Source: Internet
Author: User


Introduced in Java, JSON strings and Java objects to convert the example of a detailed explanation, very good, with reference value

In the development process, often need to exchange data with other systems, Data Interchange Format has XML, JSON and so on, JSON as a lightweight data format than XML more efficient, XML requires a lot of tags, which undoubtedly occupy the network traffic, JSON is doing a good job in this respect, Let's look at the format of JSON first,

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

{"Name": "JSON", "Address": "Xicheng District, Beijing", "age": String of 25}//json Object format

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

From the two formats above, you can see that the only difference between the object format and the array object format is the addition of [] to the object format. Then look at the specific structure, you can see that all are in the form of key-value pairs, the middle in the English state of the comma (,) separated.

This format is also popular when data is transmitted at the front and back end, 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 front-end use.

let's get to the bottom of the list of the interactions between JSON and Java objects in Java.

To achieve the mutual transfer between JSON and Java objects, you need a third-party jar pack, which uses the Json-lib jar package, which is downloaded with the address: 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 packages of support, you can download from the Internet, here no longer post the download address.

Json-lib provides several classes that can complete this function, for example, Jsonobject, Jsonarray. From the name of the class can be seen jsonobject conversion should be the object format, and Jsonarray conversion should be the array object (that is, with [] form).

One, Java normal objects and JSON strings of the interchange

Java Object--"string"

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28

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.