Python Basics-----JSON module use

Source: Internet
Author: User

JSON is a string that looks like a dictionary!

The dictionary key and value can be in single quotes or double quotes

Definition of Dictionary:

of the JSON string Definition:

Note: 1.json string does not have "variable name =" 2.json Checksum site: http://www.bejson.com/

How can 1.json strings be converted into dictionaries?

First Import JSON module

Then Json.loads (JSON string), it can be converted into a dictionary

2. How do dictionaries convert to strings?

Note: When the dumps method of the JSON string is converted to a string, it will be in the form of \uxxxx when it contains Chinese.

Workaround, use the JSON dumps note parameter Ensure_ascii=false

3. How the file is written to the JSON string

3.1 Convert the dictionary to a JSON string, then open the file and write to it using the Write () method.

3.2 Call Json.dump (dictionary, file), directly open the file, this method directly converts the dictionary into a JSON string, and write to the file,

Note: 1. indent= represents indentation 2. To make the file content look better, you can change the file's suffix to json

Contents of File Written:

4. Read the contents of the file and convert it into a dictionary

4.1 Open the file, read the file, and then convert it into a dictionary using the loads method. I don't know why it's wrong

4.2 Open the file, pass the load method, pass in the file, do not read automatically.

The difference between JSON's dumps (), loads () and the dump (), load () method

There is a need for s to manually write and read the file, without the JSON automatic reading of s to the file.

Python Basics-----JSON module use

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.