AutoFramework/Base/Class/Json.py

21 lines
504 B
Python

import json
def json_encode(data, indent=None):
return json.dumps(data, indent=indent)
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_decode(data_decode))