V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jul 19, 2015 · 2711 views
    This topic created in 3946 days ago, the information mentioned may be changed or developed.
    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 replies
    socrates
        1
    socrates  
       Jul 21, 2015   ❤️ 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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1173 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:22 · PVG 07:22 · LAX 16:22 · JFK 19:22
    ♥ Do have faith in what you're doing.