JSON for Java quick start summary

1、 JSON introduction

JSON (JavaScript object notation), similar to XML, is a data exchange format. For example, if Java generates a data to JavaScript, you can use JSON in addition to XML;

The advantage of JSON over XML is that it is very simple to express; Official website: http://www.json.org/

JSON is x in Ajax (that is, it can replace XML); ------- from the founder of JSON;

Note: JSON is not a document format, and there is no * JSON documents. Generally, documents in JSON format are stored in txt, while XML can be a standard;

JSON online editor: http://tools.jb51.net/tools/json/json_editor.htm

2、 JSON data structure

JSON has two data structures:

(1) Map, also known as object; {....}

(2)Array; [......]

That is, all JSON objects should be represented in these forms;

1.Map

Simply put, the map in Java is given in the form of name value pair. The name and value are separated by ":", and the two maps are separated by,,. The general expression is as follows:

{'key1':'value1','key2':'value2'}

The following is a diagram of the official website:

2.Array

It is an array in the ordinary sense. The general form is as follows:

['arr1','arr2','arr3'];

The following is a diagram of the official website:

The value in the figure can be:

Summary:

(1) There are only two kinds of JSON data structures;

(2) You can nest representations. For example, objects can be nested in array;

(3) Remember: object is represented by {} and array is represented by [];

3、 JSON and XML interoperation example

Any XML mentioned earlier can be converted into JSON package;

1. Simple XML

XML format:

JSON format:

2. Complex XML

XML format:

JSON format:

Note: attributes in XML are also represented by JSON map;

4、 JSON package

If we want to use JSON package, we can download the source code of JSON package and add these codes into eclipse project to call;

If you want to see the API documentation, see: http://www.JSON.org/java/index.html

The two most commonly used classes in JSON package are jsonobject and jsonarray, which represent two data structures respectively;

1. Jsonobject code example

2. Jsonarray code example

3. Nested jsonobject and jsonarray code instances

4. Jsonstringer code example

Jsonstringer can be used to quickly build a text in JSON format, convert it into a string, and write it to a file; Jsonstringer is a subclass of jsonwriter; Jsonstringer is usually through object() key(). value(). key(). value(). Endobject(); Object () indicates the beginning of an object, that is, adding {; Endobject() indicates the end of an object, that is, add}; Array () means to start an array, that is, add a [; endarray () means to end an array, that is, add a]; Key() means adding a key; Value () means adding a value;

Complex JSON format write

The above code generates the following JSON format:

5. Jsontokener code example

Jsontokener is used to read JSON format files; JSONObject obj = new JSONObject( new JSONTokener(java.io.Reader)); You can read a jsonobject from the file; JSONArray obj = new JSONArray( new JSONTokener(java.io.Reader)); You can read a jsonarray from the file;

Read code in complex JSON format:

Summary:

1 in Java, the string in JSON format is best represented by single quotation marks;

2. Use jsonobject + jsontokener to read JSON format file objects;

3. Use printwriter + jsonstringer to write JSON files;

Note: the original attempt to write with jsonwriter failed; Therefore, you can only use jsonstringer + printwriter to write;

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>