Deserialize a json string to an object in python

后端 未结 12 2442
囚心锁ツ
囚心锁ツ 2020-11-28 05:01

I have the following string

{\"action\":\"print\",\"method\":\"onData\",\"data\":\"Madan Mohan\"}

I Want to deserialize to a object of cla

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 05:37

    If you want to save lines of code and leave the most flexible solution, we can deserialize the json string to a dynamic object:

    p = lambda:None
    p.__dict__ = json.loads('{"action": "print", "method": "onData", "data": "Madan Mohan"}')
    


    >>>> p.action
    output: u'print'

    >>>> p.method
    output: u'onData'

提交回复
热议问题