JSON ==> Javascript Object Notation
AJAX is a form of key:value.
There are four methods of JSON:
Json.loads
Json.dumps
Understanding: Plus S is used to handle strings.
Json.load
Json.dump
Understanding: Not adding S is used to process the file
Load or loads
Convert JSON to other objects, strings, or file-related.
Dump or dumps
To convert another object or format into a JSON format
1. Conversion between strings and JSON
Example 1: Converting a Python dictionary format into a JSON string format
>>> a = dict (name= ' Linan ', age= ' + ', message= ' yes ') >>> print a{' message ': ' Yes ', ' age ': ' + ', ' name ': ' Linan '}>>> print type (a) <type ' dict ' >>>> import json>>> b=json.dumps (a) >>> Print b{"message": "Yes", "age": "All", "name": "Linan"}>>> print type (b) <type ' str ' >
Print A and print B are the same, but the format has changed
https://www.json.cn/ This site supports JSON parsing
Example 2: Converting a string into a dictionary
>>> C = json.loads (b) >>> print c{u ' message ': U ' yes ', U ' age ': U ' ", U ' name ': U ' linan '}>>> prin T type (c) <type ' Dict ' >
2. Conversion between file and JSON
The load must have been out of the file. JSON data, converted to JSON data
Dump is the writing of JSON data to a file
Converts a string to a file in JSON format
Import jsonjsondata= ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} ' ' # #字符串文件内写成三引号或者单引号with open (' A.txt ', ' W ') as F:json.dump ( Jsondata, F)
Results
The contents of the A.txt file are as follows:
"{\" a\ ": 1,\" b\ ": 2,\" c\ ": 3,\" d\ ": 4,\" e\ ": 5}"
Therefore, Json.dump () can write JSON data directly to the file.
Converts the contents of a JSON-formatted file into a string
With open (' A.txt ', ' R ') as Fr:m = Json.load (FR) print (m) print (Type (m))
Results
{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}
<type ' Unicode ' >
So, the Json.load () bar file content is converted to a Unicode data type to return
This article is from the "Note space" blog, so be sure to keep this source http://286577399.blog.51cto.com/10467610/1981382
23.23. Python JSON conversion