Python's JSON module

Source: Internet
Author: User

Concept:

  serialization (serialization): Converts the state information of an object into a process that can be stored or transmitted over a network, in a format that can be json,xml, and so on. Deserialization is the state of the deserialized object that is read from the storage area (Json,xml) and re-created.

  JSON(Java Script Object Notation): A lightweight data interaction format that is easier to read and write than XML, is easy to parse and generate, and JSON is a subset of JavaScript.

The python2.6 version begins with the JSON module, and the Python JSON module serialization and deserialization process is encoding and decoding, respectively.

    • Encoding: Converts a Python object encoding into a JSON string.
    • Decoding: Converts the JSON format string encoding into a Python object.

Specific application:

JSON offers four features: dumps, dump, loads, load

1 #dumps function2 #converts data into a string that is recognized by all programming languages in a special form3>>>ImportJSON4>>> data = ['AA','BB','cc']5>>> J_str =json.dumps (data)6>>>J_str7 '["AA", "BB", "CC"]'
1 #loads function2 #Convert a JSON-encoded string into a Python data structure3>>>J_str4 '["AA", "BB", "CC"]'5>>> mes =json.loads (J_STR)6>>>mes7['AA','BB','cc']
1 # Dump function 2 # converts data into a string that is recognized by all programming languages in a special form and writes to a file 3 with open ('d:/tmp.json'w') as F:  4     json.dump (data, F)
1 # Load Function 2 # reading data from a data file and converting a JSON-encoded string into a Python data structure 3 with open ('d:/tmp.json'R') as F:  4     data = Json.load (f)
Description

The basic types supported by JSON encoding are: None, bool, int, float, string, list, tuple, Dict.

For dictionaries, JSON assumes that key is a string (any non-string key in the dictionary is converted to a string at encoding), and to conform to the JSON specification, only Python lists and dictionaries should be encoded. In addition, it is a standard practice to define the topmost object as a dictionary in a Web application.

The JSON-encoded format is almost identical to the Python syntax, slightly different: True will be mapped to True,false will be mapped to False,none will be mapped to null, tuple () will be mapped to list [], because other languages do not have the concept of tuples, only arrays, This is the list.

1>>>ImportJSON2>>> data = {'a': True,'b': False,'C': None,'D':(), 1:'ABC'}3>>> J_str =json.dumps (data)4>>>J_str5 '{"A": true, "C": null, "D": [1, 2], "B": false, "1": "ABC"}'

Python's JSON module

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.