Python's JSON method

Source: Internet
Author: User

JSON Introduction:JSON, full name JavaScript Object Notation, is a lightweight data interchange format. The most extensive application of Json is the data format for the communication of Web servers and clients in AJAX . It is also often used in HTTP requests, so it is natural to learn a variety of json .

Dumps method

Converting a dictionary data type to a JSON string type

Example:

Import JSON

m = {' A ': 123, ' B ': ' Hahaha '}

Json_str = Json.dumps (M)

Print (JSON_STR)

Print (Type (JSON_STR))

Output:

{"A": 123, "B": "Hahaha"}

<class ' str ' >

Python format corresponds to JSON format

Python JSON

Dict Object

list, tuple array

STR, Unicode string

int, long, float number

True True

False False

None NULL

Loads method to convert the JSON format to Python format

Example:

Import JSON

Jsondata = ' {' A ': 1, ' B ': 2, ' C ': 3} '

Dict1 = Json.loads (jsondata)

Print (DICT1)

Print (Type (DICT1))

Output:

{' B ': 2, ' a ': 1, ' C ': 3}

<class ' Dict ' >

Dump method that writes data of the Python data type to the file in JSON format

Example:

Import JSON

Jsondata = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '

With open (' A.txt ', ' W ') as F:

Json.dump (JSONDATA,F)

Output:

Write content in the A.txt file

The Load method, which converts the file contents of the JSON type to the Python data format read

Example:

Import JSON

Jsondata = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} '

With open (' A.txt ', ' W ') as F:

Json.dump (JSONDATA,F)

With open (' A.txt ', ' R ') as FF:

Dict1=json.load (FF)

Print (DICT1)

Print (Type (DICT1))

Output:

{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}

<class ' str ' >


This article is from the "Coarse bread" blog, make sure to keep this source http://culiangmianbao.blog.51cto.com/10475024/1981458

Python's JSON method

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.