python 转化object 到json 然后存入redis以及从redis读取

object - > json

This is called serialization.



r = redisconnect(0)
prev_topicList_redis = r.get("BIGDATA.NEWS_CLUSTER:cn.dup_by_generator.3")
#read from redis, but the prev_topicList is a dict rather than a object 
prev_topicList = json.loads(prev_topicList_redis)

#covert the object to the json format 
topicList_json = json.dumps(topicList, default=jdefault, indent = 2, ensure_ascii=False).encode('utf-8')

r.set("BIGDATA.NEWS_CLUSTER:cn.dup_by_generator.3", topicList_json)

def jdefault(o):
    if isinstance(o, datetime):
         return o.isoformat()

    return o.__dict__

ref

展开阅读全文

没有更多推荐了,返回首页