V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
explist
V2EX  ›  Python

类对象与 JSON 串互转疑问

  •  
  •   explist · 2015-07-19 22:14:41 +08:00 · 1901 次点击
    这是一个创建于 3217 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import json as js

    class test:
    def __init__(self,List):
    self.a = List

    json_str = js.dumps(test(list('abc')),default=lambda obj: obj.a)

    test_obj = js.loads(json_str,object_hook=lambda obj : test(obj))

    print(type(test_obj)) # 为什么此处结果为list
    1 条回复
    socrates
        1
    socrates  
       2015-07-21 19:04:09 +08:00   ❤️ 1
    如文档写到中

    object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

    所以,list对象根本不会触发object_hook
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3205 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:41 · PVG 22:41 · LAX 07:41 · JFK 10:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.