Python module introduction-JSON

Source: Internet
Author: User

# Reference: the python standard library by example

12.9 JSON Module

JSON is JavaScript Object Notation. This module converts Python objects and JSON strings. Applicable to Python version 2.6 and later.
The JSON module provides an API similar to pickle to convert the python objects in the memory into JavaScript Object symbols (JSON) sequences. JSON has many language implementations (especially JavaScript ). Ajax applications are widely used between web servers and clients,

12.9.1 simple data types for encoding and decoding

JSON encoding recognizes the following types of Python: (string, Unicode, Int, float, list, tuple, and dict), for example:

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

Print 'data: ', repr (data)

Data_string = JSON. dumps (data)

Print 'json: ', data_string

Execution result:

$ Python json_simple_types.py

Data: [{'A': 'A', 'C': 3.0, 'B': (2, 4)}]

JSON: [{"A": "A", "C": 3.0, "B": [2, 4]}]

The JSON representation is similar to the Repr of Python. Note that, in the dictionary, there are rules for moving the variable object back?

Perform encoding and decoding in the following example:

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

Print 'data: ', Data

Data_string = JSON. dumps (data)

Print 'encoded: ', data_string

Decoded = JSON. Loads (data_string)

Print 'decoded: ', decoded

Print 'original: ', type (data [0] [' B '])

Print 'decoded: ', type (decoded [0] [' B '])

Execution result:

$ Python json_simple_types_decode.py

Data: [{'A': 'A', 'C': 3.0, 'B': (2, 4)}]

Encoded: [{"A": "A", "C": 3.0, "B": [2, 4]}]

Decoded: [{'A': 'A', 'C': 3.0, 'B': [2, 4]}]

Original: <type 'tuple'>

Decoded: <type 'LIST'>

Here, the tuples are: (2, 4). After encoding and decoding, the list is displayed.

12.9.2 simple data types for encoding and decoding

Another advantage of comparing JSON with pickle is that the results are more readable. The pickle function accepts multiple parameters to make the output even better. The following example provides the sorting function:

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

Print 'data: ', repr (data)

Unsorted = JSON. dumps (data)

Print 'json: ', JSON. dumps (data)

Print 'sort: ', JSON. dumps (data, sort_keys = true)

First = JSON. dumps (data, sort_keys = true)

Second = JSON. dumps (data, sort_keys = true)

Print 'unsorted match: ', unsorted = first

Print 'sorted match: ', first = Second

Execution result:

$ Python json_sort_keys.py

Data: [{'A': 'A', 'C': 3.0, 'B': (2, 4)}]

JSON: [{"A": "A", "C": 3.0, "B": [2, 4]}]

Sort: [{"A": "A", "B": [2, 4], "C": 3.0}]

Unsorted match: false

Sorted match: True

Indentation can also be used for multi-layer nested data structures:

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

Print 'data: ', repr (data)

Print 'normal: ', JSON. dumps (data, sort_keys = true)

Print 'indent: ', JSON. dumps (data, sort_keys = true, indent = 2)

Execution result:

$ Python json_indent.py

Data: [{'A': 'A', 'C': 3.0, 'B': (2, 4)}]

Normal: [{"A": "A", "B": [2, 4], "C": 3.0}]

Indent :[

{

"A": "",

"B ":[

2,

4

],

"C": 3.0

}

]

When indentation is not a negative number, it is similar to pprint.

Such a detailed output is not suitable for use in the product environment. You can adjust the output data of the separation encoding to make it even more compact than the default one.

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

Print 'data: ', repr (data)

Print 'repr (data): ', Len (Repr (data ))

Plain_dump = JSON. dumps (data)

Print 'dumps (data): ', Len (plain_dump)

Small_indent = JSON. dumps (data, indent = 2)

Print 'dumps (data, indent = 2): ', Len (small_indent)

With_separators = JSON. dumps (data, separators = (',',':'))

Print 'dumps (data, separators): ', Len (with_separators)

Execution result:

$ Python json_compact_encoding.py

Data: [{'A': 'A', 'C': 3.0, 'B': (2, 4)}]

Repr (data): 35

Dumps (data): 35

Dumps (data, indent = 2): 76

Dumps (data, separators): 29

12.9.3 encoding dictionary

The JSON dictionary key value is a string. If you try to encode a non-string as the key value, an exception will be generated, which is typeerror or valueerror, depending on whether the loaded module is a pure Python version or a C accelerated version, JSON can ignore these non-string key values.

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0, ('D',): 'd tuple'}]

Print 'first attempt'

Try:

Print JSON. dumps (data)

Struct T (typeerror, valueerror), err:

Print 'error: ', err

Print

Print 'second attempt'

Print JSON. dumps (data, skipkeys = true)

Execution result:

Import JSON

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0, ('D',): 'd tuple'}]

Print 'first attempt'

Try:

Print JSON. dumps (data)

Struct T (typeerror, valueerror), err:

Print 'error: ', err

Print

Print 'second attempt'

Print JSON. dumps (data, skipkeys = true)

12.9.4 custom type

So far, all examples Use Python built-in types, because these are supported by JSON itself. To encode a custom class, you can use either of the following methods. For example, the following objects are available:

Class myobj (object ):

Def _ init _ (self, S ):

Self. S = s

Def _ repr _ (Self ):

Return '<myobj (% s)>' % self. s

The simplest way to encode a myobj instance is to define a function to convert an unknown type to a known type. It does not need to be encoded, so it should only convert one object to another.

Import JSON

Import json_myobj

OBJ = json_myobj.myobj ('instance value goes here ')

Print 'first attempt'

Try:

Print JSON. dumps (OBJ)

Handle t typeerror, err:

Print 'error: ', err

Def convert_to_builtin_type (OBJ ):

Print 'default (', repr (OBJ ),')'

# Convert objects to a dictionary of their representation

D = {'_ class _': obj. _ class _. _ name __,

'_ Module _': obj. _ module __,

}

D. Update (obj. _ dict __)

Return d

Print

Print 'with default'

Print JSON. dumps (OBJ, default = convert_to_builtin_type)

Execution result:

$ Python json_dump_default.py

First Attempt

Error: <myobj (Instance value goes here)> Is Not JSON serializable

With default

Default (<myobj (Instance value goes here)>)

{"S": "Instance value goes here", "_ module _": "json_myobj ",

"_ Class _": "myobj "}

Create a myobj () instance and use the object_hook parameter to attach it to the loads () decoder. Such classes can be imported from the module and used to create instances.

Each dictionary call decoded from the input data streamObject_hookTo convert the dictionary to another data type.Hook FunctionProgramThe object to be received, not the dictionary.

Import JSON

Def dict_to_object (d ):

If '_ class _' in D:

Class_name = D. Pop ('_ class __')

Module_name = D. Pop ('_ module __')

Module = _ import _ (module_name)

Print 'module: ', module. _ name __

Class _ = getattr (module, class_name)

Print 'class: ', class _

ARGs = dict (key. encode ('ascii '), value)

For key, value in D. Items ())

Print 'instance ARGs: ', argS

Inst = Class _ (** ARGs)

Else:

Inst = d

Return Inst

Encoded_object = '''

[{"S": "Instance value goes here ",

"_ Module _": "json_myobj", "_ class _": "myobj"}]

'''

Myobj_instance = JSON. Loads (encoded_object,

Object_hook = dict_to_object)

Print myobj_instance

Running result:

$ Python json_load_object_hook.py

Module: json_myobj

Class: <class 'json _ myobj. myobj '>

Instance ARGs: {'s ': 'instance value goes here '}

[<Myobj (Instance value goes here)>]

Since JSON string values are converted to Unicode objects, they need to be re-encoded
Is an ASCII string.

12.9.5 encoding and decoding class

The JSON module provides encoding and decoding classes. You can use these classes to access additional APIs to customize your own behavior.

Jsonencoder uses a iteratable interface to encode the "chunks" of data, so that the entire data is expressed in the memory without any need, making it easier to write files or network sockets.

Import JSON

Encoder = JSON. jsonencoder ()

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

For part in encoder. iterencode (data ):

Print 'Part: ', part

Execution result:

$ Python json_encoder_iterable.py

Part :[

Part :{

Part: ""

Part ::

Part: ""

Part :,

Part: "C"

Part ::

Part: 3.0

Part :,

Part: "B"

Part ::

Part: [2

Part:, 4

Part:]

Part :}

Part:]

The encode () method is basically the same as ''. Join (encoder. iterencode (), but some error checks are added in advance. To encode any object, use the convert_to_builtin_type method to overload default ().

Import JSON

Import json_myobj

Class myencoder (JSON. jsonencoder ):

Def default (self, OBJ ):

Print 'default (', repr (OBJ ),')'

# Convert objects to a dictionary of their representation

D = {'_ class _': obj. _ class _. _ name __,

'_ Module _': obj. _ module __,

}

D. Update (obj. _ dict __)

Return d

OBJ = json_myobj.myobj ('internal data ')

Print OBJ

Print myencoder (). encode (OBJ)

Execution result:

$ Python json_encoder_default.py

<Myobj (internal data)>

Default (<myobj (internal data)>)

{"S": "Internal data", "_ module _": "json_myobj", "_ class __":

"Myobj "}

Decode the text and then convert the object to the dictionary. It takes a little more steps than the previous implementation.

Import JSON

Class mydecoder (JSON. jsondecoder ):

Def _ init _ (Self ):

JSON. jsondecoder. _ init _ (self,

Object_hook = self. dict_to_object)

Def dict_to_object (self, d ):

If '_ class _' in D:

Class_name = D. Pop ('_ class __')

Module_name = D. Pop ('_ module __')

Module = _ import _ (module_name)

Print 'module: ', module. _ name __

Class _ = getattr (module, class_name)

Print 'class: ', class _

ARGs = dict (key. encode ('ascii '), value)

For key, value in D. Items ())

Print 'instance ARGs: ', argS

Inst = Class _ (** ARGs)

Else:

Inst = d

Return Inst

Encoded_object = '''

[{"S": "Instance value goes here ",

"_ Module _": "json_myobj", "_ class _": "myobj"}]

'''

Myobj_instance = mydecoder (). Decode (encoded_object)

Print myobj_instance.

Execution result:

$ Python json_decoder_object_hook.py

Module: json_myobj

Class: <class 'json _ myobj. myobj '>

Instance ARGs: {'s ': 'instance value goes here '}

[<Myobj (Instance value goes here)>]

12.9.6 streams and files

The load () and dump () functions are used to read and write similar objects.

Import JSON

From stringio import stringio

Data = [{'A': 'A', 'B' :( 2, 4), 'C': 3.0}]

F = stringio ()

JSON. Dump (data, F)

Print F. getvalue ()

Execution result:

$ Python json_dump_file.py

[{"A": "A", "C": 3.0, "B": [2, 4]}]

Although not optimized to read only part of the data at a time, load () provides the logic for generating objects from stream input.

Import JSON

From stringio import stringio

F = stringio ('[{"A": "A", "C": 3.0, "B": [2, 4]}]')

Print JSON. Load (f)

Execution result:

$ Python json_load_file.py

[{'A': 'A', 'C': 3.0, 'B': [2, 4]}]

12.9.7 hybrid data stream

Jsondecoder includes raw_decode (). This method can decode the data structure containing more data.
For example, JSON data with subsequent text. The returned value is the decoded input data and index.

(OBJ, end, remaining)

Encoded_object = '[{"A": "A", "C": 3.0, "B": [2, 4]}]'

Extra_text = 'this text is not JSON .'

Print 'json first :'

Data = ''. Join ([encoded_object, extra_text])

OBJ, end, remaining = get_decoded_and_remainder (data)

Print 'object: ', OBJ

Print 'end of parsed input: ', end

Print 'remaining text: ', repr (remaining)

Print

Print 'json embedded :'

Try:

Data = ''. Join ([extra_text, encoded_object, extra_text])

OBJ, end, remaining = get_decoded_and_remainder (data)

Failed t valueerror, err:

Print 'error: ', err

Execution result:

$ Python json_mixed_data.py

JSON first:

Object: [{'A': 'A ', 'C': 3.0,' B ': [2, 4]}]

End of parsed input: 35

Remaining text: 'this text is not JSON .'

JSON embedded:

Error: No JSON object cocould be decoded

Note: The object must start with the input.

Other references:

JSON (http://docs.python.org/library/json.html)

JavaScript Object Notation (http://json.org /)

Simplejson (http://code.google.com/p/simplejson)

Simplejson (http://code.google.com/p/simplejson)

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.