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