JSON modules in Python

Source: Internet
Author: User
Tags compact

JSON templates in Python main two features: serialization and deserialization
Serialization: Encoding to encode Python data as a JSON string
The corresponding function has dump and dumps
Deserialization: Decoding decodes JSON strings into Python data
The corresponding function has load and loads


JSON serialized dumps instance:

Base Example

>>> import json>>> data=[' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}]>>> print data[' foo ', {' Bar ': (' Baz ', None, 1.0, 2)}]>>> json_data=json.dumps (data) >>> print json_data["foo", {"bar": ["Baz", NULL, 1.0, 2]}]>>>


Compact encoding (compression coding)

>>> Import json>>> data = [1,2,3,{' 4 ': 5, ' 6 ': 7}]>>> print data[1, 2, 3, {' 4 ': 5, ' 6 ': 7}]> >> Data_json = json.dumps (data) >>> print data_json[1, 2, 3, {"4": 5, "6": 7}]>>> Data_json2 = JSON . Dumps (data,sort_keys=true) >>> print data_json2[1, 2, 3, {"4": 5, "6": 7}]>>> Data_json2 = Json.dumps (d Ata,sort_keys=true,separators= (', ', ': ')) >>> print data_json2[1,2,3,{"4": 5, "6": 7}]


Parameter separators, and: The Back door space is removed. The value of the separators must be a tuple
English comments in the Help:
If specified, separators should be a (item_separator, key_separator) tuple.

The default is (', ', ': '). To get the most compact JSON
Representation should specify (', ', ': ') to eliminate whitespace.


Pretty printing (a formatted output)

>>> Data_json3 = json.dumps (data,sort_keys=true,indent=4,separators= (', ', ': ')) >>> print Data_ json3[1, 2, 3, {"4": 5, "6": 7}]


Indent causes each key-value pair to be displayed with a indent of several characters to align. for easy viewing
English comments in the Help:
If indent is a non-negative integer, then JSON array elements and object members'll be pretty-printed with that indent l  Evel. An indent level of 0 would only insert newlines.  None is the most compact representation.  Since The default item separator is ', ', the output might include trailing whitespace when indent is specified. You can use the separators= (', ', ': ') to avoid this.


Josn deserialization Loads instance:

>>> obj = [u ' foo ', {u ' bar ': [u ' Baz ', None, 1.0, 2]}]>>> Str_json = ' [' foo ', {' bar ': [' Baz ', NULL, 1.0, 2] }] ' >>> obj2 = json.loads (Str_json) >>> print obj2[u ' foo ', {u ' bar ': [u ' Baz ', None, 1.0, 2]}]>>> Obj2 = = Objtrue


Big Data processing:
The dumps and deserialization of the loads, regardless of the time of serialization. The data being targeted is either a JSON string or a Python data structure. So when a lot of JSON data is encountered (such as a JSON configuration file)
Or export a Python data structure to a JSON configuration file.

#! /usr/bin/env python# _*_ encoding:utf-8 _*_import json# Dump exampledata = [{' lang ':(' python ', ' Java '), ' School ': ' Beijing "}," God "]f = open (' Test.json ', ' w+ ') json.dump (data,f) F.flush () f.close () # Load EXAMPLEFD = File (" Test.json ") js = Json.load (FD) Print JS



Odd Kinky Inventions:

Python json combined with shell output

$ Echo ' {' json ': ' obj '} ' | Python-m Json.tool {"json": "Obj"}


This article is from the "Learning Notes" blog, so be sure to keep this source http://unixman.blog.51cto.com/10163040/1649960

JSON modules in Python

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.