Servlet,jdbc,jsonobject Match and process client requests and return the correct JSON data

Source: Internet
Author: User

First of all we understand the JSON (JavaScript Object Notation), of course, this blog is not intended to re-introduce, if you are familiar with the Python dictionary structure and list structure, in fact, the JSON format is very easy to understand, of course, is not familiar and not difficult to understand, a lot of online information. can be downloaded to http://www.json.org/java/index.html to parse the code we want to use, and finally I will explain the specific process of parsing.

Use of the JSON package

We can use ToString () to replace these objects with strings.

J Sonobject

Note that its use is very similar to a map, because the string corresponding to the Jsonobject class can actually be thought of as a dictionary structure (just like the dictionary format printed in Python)

Using the put (string,string) or constructor, add a key-value pair to the JSON

Using put (String, new string[]{}) to add a key-value pair, the value is an array

Use put (string, map) to add a key-value pair, which is a string that satisfies the JSON format

Use GetString (key) to return a value, which is, of course, converted to a string.

Jsonarray

  Its usage is similar to list, if only need to return a [...] form of the string, this method is good.

Use the static method Fromobject (new string[]{}) or Fromobject (list) to get an object of this class, corresponding to an array string.

Add an element using Add (String) or add (index,string)

Add a JSON-formatted string with Add (jsonobj)

tips: Adding a lot of nesting although theoretically feasible, there is absolutely no need to be so complex in practical applications, simple and easy to interpret is the best.

Process Analysis

The Web server encapsulates the HTTP request message as an object and executes our servlet code as a parameter, so how do we write our code? Naturally, we are going to use JDBC to read the data and then to store the data in a dictionary or array structure, build the Jsonobect object with the help of a JSON package, and then replace it with a string to write to the HTTP report style.

Now give a relational pattern in the database,

JSON package source Code Analysis

  

Servlet,jdbc,jsonobject Match and process client requests and return the correct JSON data

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.