2

我对 Python 相当陌生,在我的应用程序中,我使用 Neomodel 从 Neo4j 数据库加载和检索图形数据。

在我的一条路线中,我具有以下功能:

@api_mod.route('/users')                                                            
def get_users():                                                                                                                                                        
     users = User.nodes #Returns a NodeSet                                                                                                                              
     list_of_users = list(users) #Converts the NodeSet object to a list                                                                                                                                                                                                                                                                    
     return json.dumps(dict(users = [user for user in list_of_users]))    

我的用户类定义如下:

class User(StructuredNode):
    user_id = StringProperty(unique_index=True, required=True)
    logged_in = RelationshipTo('Environment', 'LOGGED_IN', model=LoginAction)
    launched = RelationshipTo('Application', 'LAUNCHED', model=LaunchedAction)
    entered = RelationshipTo('Application', 'ENTERED', model=EnteredAction)
    accessed = RelationshipTo('Application', 'ACCESSED', model=AccessedAction)
    exited = RelationshipTo('Application', 'EXITED', model=ExitedAction)
    logged_out = RelationshipTo('Environment', 'LOGGED_OUT', model=LogoutAction)
    timed_out = RelationshipTo('Environment', 'TIME_OUT', model=TimeoutAction)

    def toJSON(self):
        return dict(user_id = self.user_id)

我的期望是我的 /users 路由将返回一个包含数据库中所有用户的 JSON 对象。我知道 NodeSet 对象不能序列化为 JSON 对象,因此我尝试将其转换为列表。但是现在当我运行时,我收到以下错误:“TypeError:'User' 类型的对象不是 JSON 可序列化的”

这是整个堆栈跟踪: Traceback(最近一次调用最后一次):文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1997 行,调用中 返回 self.wsgi_app(environ, start_response) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第 1985 行,在 wsgi_app response = self.handle_exception(e )文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,第1540行,在handle_exception reraise(exc_type,exc_value,tb)文件“/Users/mdebarros/ .virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/_compat.py”,第 33 行,在 reraise raise value 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages /flask/app.py”,第 1982 行,在 wsgi_app 响应 = self.full_dispatch_request() 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”,行1614,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/app.py”中,第 1517 行,在 handle_user_exception reraise(exc_type, exc_value, tb)文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages/flask/_compat.py”,第 33 行,在 reraise raise value 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/ lib/python3.6/site-packages/flask/app.py”,第 1612 行,在 full_dispatch_request rv = self.dispatch_request() 文件“/Users/mdebarros/.virtualenvs/useractivitylogs/lib/python3.6/site-packages /flask/app.py”,第 1598 行,在 dispatch_request 返回 self.view_functionsrule.endpoint 文件“/Users/mdebarros/PycharmProjects/useractivitylogs/app/api/routes.py”,第 16 行,在 get_users 返回 json.dumps(dict(users = [user for user in list_of_users])) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6 /json/init .py”,第 231 行,在转储中返回 _default_encoder.encode(obj) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/ encoder.py”,第 199 行,在编码块 = self.iterencode(o, _one_shot=True) 文件中“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3 .6/json/encoder.py”,第 257 行,在 iterencode 返回 _iterencode(o, 0) 文件“/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3 .6/json/encoder.py",第 180 行,默认o.class .name ) TypeError: 'User' 类型的对象不是 JSON 可序列化的

关于如何处理此问题并返回正确的 JSON 对象的任何帮助或建议?

提前感谢您的任何帮助。

--MD

4

1 回答 1

1

如您所知,用户类 Model 必须有一个方法来执行数据结构的转换(序列化)过程,要解决,解决方案是添加一个方法来执行此操作:

class User(StructuredNode):
    user_id = StringProperty(unique_index=True, required=True)
    logged_in = RelationshipTo('Environment', 'LOGGED_IN', model=LoginAction)
    launched = RelationshipTo('Application', 'LAUNCHED', model=LaunchedAction)
    entered = RelationshipTo('Application', 'ENTERED', model=EnteredAction)
    accessed = RelationshipTo('Application', 'ACCESSED', model=AccessedAction)
    exited = RelationshipTo('Application', 'EXITED', model=ExitedAction)
    logged_out = RelationshipTo('Environment', 'LOGGED_OUT', model=LogoutAction)
    timed_out = RelationshipTo('Environment', 'TIME_OUT', model=TimeoutAction)

@property
def serialize(self):
    return {
        'user_id': self.user_id,
        'logged_in': self.logged_in,
        'launched': self.launched,
        'entered': self.entered,
        'accessed': self.accessed,
        'exited': self.exited,
        'logged_out': self.logged_out,
        'timed_out'  : self.timed_out
   }

现在,由于该方法已作为属性添加,因此可以称为属性:

@api_mod.route('/users')                                                            
def get_users():                                                                                                                                                        
    users = User.nodes                                                                                                                            
    list_of_users = list(users)
    return json.dumps(dict(json_users = [user.serialize for user in list_of_users]))
于 2017-12-27T05:21:53.730 回答