ホームページ  >  に質問  >  本文

eclipse - python如何把json字符串转换成自定义的对象

python如何把json字符串转换成自定义的对象

class Result(object):

    def __init__(self, code,message, response=[]):
        self.code=code
        self.message = message
        self.response = response

这是json字符串:str: {"code":200,"message":"发送成功","response":[{"code":2,"message":"xxxxxxxx","mobile":"xxxxxx","taskId":null},{"code":2,"message":"xxxxxx","mobile":"xxxxxx","taskId":null}]}

PHP中文网PHP中文网2741日前308

全員に返信(1)返信します

  • PHP中文网

    PHP中文网2017-04-18 09:04:58

    json.loads()

    https://docs.python.org/2.7/library/json.html

    リーリー

    object_hook を追加します

    質問を読んだ後、object_hook は必要ありません。

    シンプルかつ粗野

    リーリー

    更新: 形式を変更してから、response=[]この書き方は推奨されません

    利用可能な最後の結果:

    リーリー

    返事
    0
  • キャンセル返事