1
我使用的串行jsonpickle當我全選到數據庫,我要列出對象轉換爲JSON時使用jsonpickle爲了這個,我得到這個輸出:如何在sqlalchemy中序列化列表對象?
代碼:
Session = sessionmaker(bind=connect.ConnectorMySql())
ses = Session()
lst = ses.query(pModel).all()
return lst
jsonpickle:
serialized_obj = jsonpickle.encode(lst[0])
輸出:
{"py/object": "Models.UserModel.", "sa_instance_state": {"py/object": "sqlalchemy.orm.state.InstanceState", "py/state": {"instance": {"py/id": 0}, "committed_state": {}, "class": {"py/type": "Models.UserModel."}, "manager": {"py/object": "sqlalchemy.orm.instrumentation.SerializeManager", "class": {"py/type": "Models.UserModel.Student"}}, "key": {"py/tuple": [{"py/type": "Models.UserModel.Student"}, {"py/tuple": [1]}]}, "expired_attributes": {"py/set": []}}}, "name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
但我需要這種格式json:
{"name": "ramin", "fullname": "eee", "password": "1234345", "id": 1}
你覺得這個工作更好嗎?
可以請你發佈你的代碼嗎? – NiviD
好的,請再看一遍 –