Conversion between JSON strings and Java objects

Source: Internet
Author: User

I have previously written an article about gson's simple usage.ArticleToday, I want to write an article about how to explain JSON data without Google's gson.

We all know that before gson is available, Java will have JSON transmission and parsing. How does it complete ???

Before entering the topic, we now download some jar packages

These resources will be uploaded to my resource page soon. You are welcome to download them ....

Now let's create a Java project.

The object "person" contains only the string name and string age attributes and their set and get methods.

The key is the jsontest class.

First create a jsonobject object

Jsonobject JSON = new jsonobject ();

Create a person object

Person = newperson ("yang", "14 ");

Then use the following sentenceCodeConverts a Java object to a JSON object.

JSON = jsonobject. formobject (person );

To convert a JSON string to a Java object

You can use the following code

Jsonobject. tobean (JSON, person. Class );

So that the conversion between the JSON string and the Java object can be realized ~~ Heheh

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.