CSDN博客地址

anyjson与simplejson的使用

import simplejson
import anyjson

# 序列化: 把变量从内存中变成可存储或可传输的过程

def any_1():
    # 序列化
    info = {"a": 1, "b": 2}
    result = anyjson.serialize(info)
    f = open("file.text", "w")
    f.write(result)
    f.close()
def any_2():
    # 反序列化
    with open("file.text", "r") as f:
        info = anyjson.deserialize(f.read())
    print(info)

def smp_1():
    # 序列化
    # 方式一
    """info = {"a":1, "b": 2}
    result = simplejson.dumps(info)
    with open("122.text", "w") as f:
        f.write(result)"""
    # 方式二
    info = {"a": 1, "b": 2}
    with open("122.text", "w") as f:
        simplejson.dump(info, f)



def simp_2():
    # 反序列化
    # 方式一
    """with open("122.text", "r") as f:
        info = f.read()
        result = simplejson.loads(info)
    print(result)"""
    # 方式二
    with open("122.text", "r") as f:
        info = simplejson.load(f)
    print(info)

# 排序
a = {"c": 1, "b": 2}
info = simplejson.dumps(a, sort_keys=True)
print(info)   # {"b": 2, "c": 1}

 

posted @ 2020-06-19 20:01  Yi_warmth  阅读(250)  评论(0编辑  收藏  举报
CSDN博客地址