ResultSet into JSON (Java) in servlet

Source: Internet
Author: User

Recently in doing Android pull data from the server, using the Doget way to request data from the servers. The resultset result set that the server has obtained after the SQL query is returned in JSON form. So focus on how the dataset is encapsulated into JSON, and how the client can reorganize the returned JSON data stream into the original data

Let's start by saying something about JSON:

The JSON format is essentially a string stream, except that it has a JSON-specific format: [{},{},{}...,{}], the brackets [] represent the jsonarray, and the curly braces {} represent the Jsonobject, JSON format of the specific situation readers can Baidu, it is easy to understand

Start to the chase:

First look at the structure of the data:

As a demonstration, the following data sets are used:

That is: data in the following structure

{"title": "This is a heading 0", "image": "This is picture 0", "info": "This is a more information 0"},

{"title": "This is a Heading 1", "image": "This is picture 1", "info": "This is a more information 1"},

{"title": "This is a Heading 2", "image": "This is Picture 2", "info": "This is a more information 2"},

{"title": "This is a Heading 3", "image": "This is picture 3", "info": "This is a more information 3"},

{"title": "This is a Heading 4", "image": "This is picture 4", "info": "This is a more information 4"},

{"title": "This is a Heading 5", "image": "This is picture 5", "info": "This is a more information 5"},

........................... (a total of 20 groups)

The database executes the query after the resultset, its structure as shown above, the specific data will be different, the above is just an example of convenience, the following operation of the reader can self-analogy

Service side:

1. Extract the data from the resultset into the list<map<string, object>> structure:

list<map<string, object>> data = new arraylist<map<string, object>> ();

2. Convert data to Jsonarray

3. After converting data to Jsonarray, return to the client:

Client:

The JSON data received is in the form of a stream, and the next thing to do is restore the server in a JSON-formatted resultset, and reorganize the JSON stream into list<map<string, object>> data

1. In Executehttpget, get the received value in JSON format, in a variable of type string, assuming that the variable is: receivestring

2. Convert the receivestring to Jsonarray:

3.reJsonArray contains the above 20 jsonobject, so now it is necessary to traverse the Rejsonarray, each jsonobject carried in the key-value pair into map<>, if there is confusion, First look at the above storage structure, and then combined with the following code is very good understanding:

4. Following these three steps, the JSON stream is reverted to the list<map<string, object>> structure

In summary, the change in the form of data has undergone a process such as:

Service-side query out resultset------>>> resultset deposit list<map<string, object>> structure.

------>>> Returns the data in the list<map<string, object>> structure to the JSON format to return the client.

------>>> client receives the JSON stream. ------>>> Restore the JSON stream to list<map<string, object>> structure for use

Finally put my running result diagram: (Get this list<map<string, object>> structure behind the operation really flexible a lot, how to come to how to come)

ResultSet into JSON (Java) in servlet

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.