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