Use JSON in Java
I. Introduction to JSON
JSON (JavaScript Object Notation), similar to XML, is a data exchange format. For example, if JAVA generates a data to JavaScript, JSON can be used in addition to XML;
JSON has two data structures, that is, all JSON objects must be expressed in these forms;
(1) Map, also known as an object ;{....}
(2) Array; [...]
1. Map
In short, Map in Java is given in the form of name-value pairs. Names and values are separated by ":", and two maps are separated by ",". The general representation is as follows:
{'Key1': 'value1', 'key2': 'value2 '}
2. Array
It is an array in the general sense, the general form is as follows:
['Arr1', 'arr2', 'arr3'];
Summary: (1) there are only two types of JSON data structures;
(2) nested representation, such as nested Object in Array;
(3) Remember: The Object is represented by {}, and the Array is represented;
For details, refer to the official website, which provides an intuitive graphical type explanation.
2. Use JSON in JAVA
In terms of interface access and data transmission, JSON objects are used to format data: transmit data using JSON strings on the server side and parse the received data using JSON on the WEB Front-end or Android client.
First, to use JSON in JAVA, You need to introduce the org. json package and introduce the corresponding JSON class in the program:
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
Second, you can use the following methods to collect data and generate the corresponding JSON string in the server-side Servlet class:
- // Declare a Hash object and add data
- Map params = new HashMap ();
- Params. put ("username", username );
- Params. put ("user_json", user );
- // Declare the JSONArray object and input the JSON string
- JSONArray array = JSONArray. fromObject (params );
- Put. println (array. toString ());
On the WEB front-end, JSON strings can be directly parsed using javascript. On the Android client, JSON classes are used to parse strings:
- // @ Description: parses the data and data objects contained in the string based on the received JSON string.
- // The received JSON string
- String result = "[{\" username \ ": \" your name \ ", \" user_json \ ": {\" username \ ": \" your name \", \ "nickname \": \ "your nickname \"}] ";
- // Generate a JSON object based on the string
- JSONArray resultArray = new JSONArray (result );
- JSONObject resultObj = resultArray. optJSONObject (0 );
- // Obtain data items
- String username = resultObj. getString ("username ");
- // Obtain the data object
- JSONObject user = resultObj. getJSONObject ("user_json ");
- String nickname = user. getString ("nickname ");
For detailed usage, refer to the official document http://www.json.org/javadoc/org/json/package-tree.html Class Hierarchy
- Java. lang. Object
- Org. json. CDL
- Org. json. Cookie
- Org. json. CookieList
- Org. json. HTTP
- Org. json. JSONArray
- Org. json. JSONML
- Org. json. JSONObject
- Org. json. JSONTokener
- Org. json. HTTPTokener
- Org. json. XMLTokener
- Org. json. JSONWriter
- Java. lang. Throwable (implements java. io. Serializable)
- Org. json. XML
Interface Hierarchy