Java EE Seventh Blog----JSON

Source: Internet
Author: User
Tags closing tag compact

First, the JSON basic introduction:

1. JSON is a text-based data interchange format that comes from JavaScript used in Web services and other connected applications

2. JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines 7 types of values: string, number, object, array, True, false, and null.

3. JSON is often used as a common format for serializing and deserializing data in applications that communicate on the Internet. These applications are created using different programming languages and run in very different environments. JSON is suitable for this scenario because it is an open standard, it is easy to read and write, and it is more compact than other representations.

4. The JSON representation is usually more compact than the XML representation because the JSON does not have a closing tag. Unlike XML, JSON does not have a widely accepted pattern to define and validate the structure of JSON data.

5. The object model creates a tree that represents JSON data in memory. You can then navigate, analyze, or modify the tree. This approach is the most flexible and allows for processing, which requires access to the full contents of the tree. However, it is usually slower than the flow model and requires more memory. The object model generates JSON output by navigating the entire tree at once.

6. The stream model uses the event-based parser to read only one element of JSON data at a time. When an object or array begins or ends, when it finds a key, or when it finds a value, the parser generates an event and stops processing. The application code can process or discard each element, and the parser proceeds to the next event. This approach is appropriate for local processing, where the processing of one element does not require information from other data. The flow model uses one element at a time to invoke a function to generate the JSON output to the given stream.

JSON is constructed in two structures:

A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).

The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).

These are common data structures. In fact, most of the modern computer languages support them in some form. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs.

1. Encoding and decoding for simple data types:

Json.dumps (obj, Skipkeys=false, Ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=None, Separators=none, encoding= "Utf-8", Default=none, Sort_keys=false, **kw)

Print Json.dumps ([' foo ', {' bar ':(' baz ', none,1.0,2)}])

Print json.dumps ("\" Foo\bar ")

Print json.dumps (U ' \u1234 ')

Print json.dumps (' \ \ ')

Print Json.dumps ({"C": 0, "B": 0, "a": 0},sort_keys=true)

Output:

["foo", {"bar": ["Baz", NULL, 1.0, 2]}]

"\" Foo\bar "

"\u1234"

"\\"

{"A": 0, "B": 0, "C": 0}

Compact encoding:

#json主要是作为一种数据通信的格式存在的, and network communication is very concerned about the size of the data, useless space will occupy a lot of communication bandwidth, so the appropriate time also to compress the data. The separator parameter can play the role of a tuple that contains a string that splits the object.

Print Json.dumps ([1,2,3,{' 4 ': 5, ' 6 ': 7}],separators= (', ', ': '))

#indent参数是缩进的意思, it can make the format of data storage more elegant.

Print Json.dumps ([1,2,3,{' 4 ': 5, ' 6 ': 7}],separators= (', ', ': '), indent = 4)

2.decoding JSON

Json.load (fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]] [] ]]) www.2cto.com

Print json.loads (' [' foo ', {"bar": ["Baz", null,1.0,2]}] ')

Print json.loads (' "\ \" Foo\\bar ")

Output:

[u ' foo ', {u ' bar ': [u ' Baz ', None, 1.0, 2]}]

"Foar

3. Parsing of special JSON objects

def as_complex (DCT):

If ' __complex__ ' in DCT:

Return complex (dct[' real '],dct[' imag ')

Return DCT

Print Json.loads (' {"__complex__": true, "real": 1, "Imag": 2} ', Object_hook=as_complex)

Output:

(1+2J)

Java EE Seventh Blog----JSON

Related Article

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.