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

来源:互联网 发布:h5游戏源码搭建 编辑:程序博客网 时间:2024/06/04 08:59

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
  • http://liuzhijun.iteye.com/blog/1859857
  • http://stackoverflow.com/questions/1458450/python-serializable-objects-json
0 0