AutoFramework/Base/Class/Json.py

22 lines
601 B
Python
Raw Normal View History

2022-07-17 01:36:33 +08:00
import json
2022-07-21 08:44:10 +08:00
def json_encode(data, indent=None, unicode=True):
return json.dumps(data, indent=indent, ensure_ascii=unicode)
2022-07-17 01:36:33 +08:00
def json_decode(data):
return json.loads(data)
if __name__ == '__main__':
# Example.
data_encode = {"id": 101, "name": "小明", "friends": ["小红", "小花"]}
data_decode = """
{"id": 101, "name": "\u5c0f\u660e", "friends": ["\u5c0f\u7ea2", "\u5c0f\u82b1"]}
"""
print(json_encode(data_encode))
print(json_encode(data_encode, indent=4))
2022-07-21 08:44:10 +08:00
print(json_encode(data_encode, indent=4, unicode=False))
2022-07-17 01:36:33 +08:00
print(json_decode(data_decode))