Python's json module provides a very simple way to encode and decode JSON data. The two main functions are json.dumps() and json.loads().
json.dumps() converts plain dictionaries in Python into JSON-encoded strings.
json.loads(): Contrary to the dumps method, converts JSON-encoded strings into pure dictionaries in python.
What about JSON format files, dictionaries in Python, and JSON-encoded strings? The following are examples of the three data types:
JSON format file: {"phone": "13011110000", "type": 1}
Dictionary in python: {'phone': '13011110000', 'type': 1}
JSON encoding String: '{"phone": "13011110000", "type": 1}'
Conversion between JSON-encoded string and dictionary data:
# coding = utf-8
import json
# 将字典数据转换成JSON编码的字符串
data = {
'phone': '13011110000', 'type': 1}
json_str = json.dumps(data)
print(json_str, type(json_str))
# coding = utf-8
import json
# 将JSON编码的字符串转换成字典数据
data = '{"phone": "13011110000", "type": 1}'
json_dict = json.loads(data)
print(json_dict, type(json_dict))