AutoFramework/Base/Class/Json.py

22 lines
601 B
Python

import json
def json_encode(data, indent=None, unicode=True):
return json.dumps(data, indent=indent, ensure_ascii=unicode)
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))
print(json_encode(data_encode, indent=4, unicode=False))
print(json_decode(data_decode))