您的位置:首页 > 数据库 > Redis

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

2015-08-19 16:28 686 查看

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

http://pythontips.com/2013/08/08/storing-and-loading-data-with-json/

http://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object

/article/3898678.html

http://stackoverflow.com/questions/1458450/python-serializable-objects-json
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: