Python's summary of JSON operations (i)

Source: Internet
Author: User

Json

concept:JSON is a lightweight data interchange format.

If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.

format: an unordered collection of ' name '/' values '. {Name 1/value, name 2/value ...}.

Description: You may be reminded of where this format has been seen, yes. In Python, dictionaries also have similar key:value structures. But you can't confuse it.

    • The JSON key can only be a string, and Python's dict could be any hash object
    • The JSON key can be ordered.
    • The JSON key can be repeated
    • The values of JSON can only be strings, floating-point numbers, Boolean values, or null, or arrays or objects that they make up.

Encoding-Serialization:

    • Dumps: Converting data types to strings
    • Dump: Converts a data type into a string and stores it in a file

Decode--Deserialization:

    • Loads: Converting a string to a data type
    • Load: Convert file to string from data type

Note: All variables are in memory during the program's run. Once the program is finished, the variables will disappear.

So, the process of turning a variable from memory into a storage or transfer is called serialization. in turn, re-reading the variable contents from the serialized object into memory is called deserialization.

1. Dumps: Convert dictionary to string

in [+]: d=dict (name='xjm') in [101]: a=json.dumps (d) in [  102]: aout[102'{"name": "XJM"}'in[ 103 ]: Type (a) out[103]: Str

2. Dump: Writing to the JSON file

In [MAX]: with open ('desktop/j.txt','w') as F:     ...:     a=json.dump (d,f) ...     :print(' file write complete ... ' )     ...: File write complete ...

3. Loads: Read JSON file

In [the]: d=json.loads (a) in [+]: dout[]: {'name'XJM  '}

4. Load: Read JSON file in file

in [+]: with open ('j.txt','rb') as F:     ...:     a =json.load (f) ...     : in [121]: aout[121]: {'name' ' XJM '}

Python's summary of JSON operations (i)

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.