Json encoding and parsing

Source: Internet
Author: User

JSON is short for JavaScript Object Notation, and SJON is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. 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 an object, record, struct, dictionary, and hash table ), keyed list or associative array ). An ordered list of values ). In most languages, it is understood as an array ). These are common data structures. In fact, most modern computer languages support them in some form. This makes it possible to exchange a data format between programming languages that are also based on these structures. 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 is mainly used as a data communication format. network communication is very important to the data size, and useless spaces occupy a lot of communication bandwidth, therefore, data must be compressed as appropriate. The separator parameter can be used to specify a delimiter that contains the string of the object to be separated. Print json. dumps ([1, 2, 3, {'4': 5, '6': 7}], separators = (',',':')) # The indent parameter indicates indentation, which makes the data storage format more elegant. Print json. dumps ([1, 2, 3, {'4': 5, '6': 7}], separators = (',', ':'), indent = 4) 2. decoding JSONjson. 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]}] "foar3. 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)

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.