>  Q&A  >  본문

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일 전311

모든 응답(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
  • 취소회신하다