Use JSON in Java

Source: Internet
Author: User

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:
  1. // Declare a Hash object and add data
  2. Map params = new HashMap ();

  3. Params. put ("username", username );
  4. Params. put ("user_json", user );

  5. // Declare the JSONArray object and input the JSON string
  6. JSONArray array = JSONArray. fromObject (params );
  7. 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:
  1. // @ Description: parses the data and data objects contained in the string based on the received JSON string.

  2. // The received JSON string
  3. String result = "[{\" username \ ": \" your name \ ", \" user_json \ ": {\" username \ ": \" your name \", \ "nickname \": \ "your nickname \"}] ";

  4. // Generate a JSON object based on the string
  5. JSONArray resultArray = new JSONArray (result );
  6. JSONObject resultObj = resultArray. optJSONObject (0 );

  7. // Obtain data items
  8. String username = resultObj. getString ("username ");

  9. // Obtain the data object
  10. JSONObject user = resultObj. getJSONObject ("user_json ");
  11. 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
      • Org. json. JSONStringer
    • Java. lang. Throwable (implements java. io. Serializable)
      • Java. lang. Exception
        • Org. json. JSONException
    • Org. json. XML
Interface Hierarchy
  • Org. json. JSONString

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.