TypeError:<neo4j.work.result.result对象在0x7f3f4bd01470>处不可JSON序列化

2024-10-02 20:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

对于下面的代码,我得到一个错误,请告诉我如何解决这个问题

class GenerateQuery:

    @staticmethod
    def get_nlg(graph_query):
#        graph = Graph("http://localhost:7474",auth=("neo4j", "pass"))
#        graph_response = graph.evaluate(graph_query)
#        return graph_response
        driver = GraphDatabase.driver("neo4j://localhost:7687", auth=("neo4j","pass"))
        with driver.session() as session:
            graph_response = session.run(graph_query)
            return graph_response

    @staticmethod
    def product_review(summary_comp,prod_comp):
        """
        :param summary_comp: product summary
        :param prod_comp: product node name
        :return: Summary/Review of the corresponding product
        """
        query = u'MATCH(s:Store)<-[r:REVIEWED]-(c:Customer) RETURN s.name as ProductName, r.summary as ProductReview'
        graph_response = GenerateQuery.get_nlg(query)
        return graph_response

当上述结果传递给下面的代码时,它会给出一个错误:

class ProductReview(Action):
    def name(self):
        return "action_review"

    def run(self, dispatcher, tracker, domain):
        intent = tracker.latest_message['intent']
        summary_comp = tracker.get_slot('summary')
        prod_comp = tracker.get_slot('node')
        graph_response = GenerateQuery.product_review(summary_comp,prod_comp)
        dispatcher.utter_message(json.dumps(graph_response))

错误是:

Traceback (most recent call last):
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/app.py", line 939, in handle_request
    response = await response
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 112, in webhook
    return response.json(result, status=200)
  File "/home/sangeetha/Desktop/RiQue/venv/lib/python3.6/site-packages/sanic/response.py", line 210, in json
    dumps(body, **kwargs),
TypeError: <neo4j.work.result.Result object at 0x7f3f4bd01470> is not JSON serializable

Tags: getreturnresponsedefdriver错误prodsummary
1条回答
网友
1楼 · 发布于 2024-10-02 20:34:18

Result并不意味着要序列化,它保存事务终止时释放的事务绑定数据

在序列化数据之前,必须先提取数据。 您可以使用以下内容更改get_nlg

return [record.data() for record in graph_response]

作为旁注,session.run最好用session.read_transaction(又称a transaction function)代替

相关问题 更多 >