【问题标题】:outputiing py2neo query as JSON将 py2neo 查询输出为 JSON
【发布时间】:2016-07-25 20:36:22
【问题描述】:

我已经尝试过四处寻找,但无济于事。以下是我在 py2neo 中的密码查询:

graph = Graph()
In [6]: query = """Match (C:Customer)-[r:Customer_Send]->(Send:Customer) where C.Galactic_ID = '2000000000084001287' return Send.Galactic_ID """

In [7]: graph.cypher.execute(query)


Out[7]:
       | Send.Galactic_ID
 ----+---------------------
      1 | 2000000000084114531
      2 | 1000000000284949451
      3 | 2000000000084114531
      4 | 1000000000213446086

我希望上述输出为 JSON 格式。

【问题讨论】:

    标签: json neo4j py2neo


    【解决方案1】:

    仅使用标准库和最新版本的 py2neo,这相当简单:

    >>> from py2neo import Graph
    >>> from json import dumps
    >>> g = Graph(password="password")
    >>> dumps(g.run("UNWIND range(1, 3) AS n RETURN n").data())
    '[{"n": 1}, {"n": 2}, {"n": 3}]'
    

    http://py2neo.org/v3/database.html#py2neo.database.Cursor.data

    【讨论】:

    • 嗨奈杰尔。这真的很有帮助。直到现在我还在使用旧版本。感谢您的提示。
    【解决方案2】:

    以下是这样做的方法:我相信这将对这里的读者有巨大的帮助:

    query = """Match (C:Customer)-[r:Customer_Send]->(Send:Customer) where C.Galactic_ID = '2000000000084001287' return Send.Galactic_ID as ID"""
    
    records = graph.cypher.execute(query,Customer='ID')
    returnObject = []
    
    In [11]: for record in records:
     ....:     returnObject.append(
     .  ...:     {
      ....:     'Customer':record.ID
         ....:     }
           ....:     )
    
    
    In [15]: returnObject
    Out[15]:
      [{'Customer': u'2000000000084114531'},
      {'Customer': u'1000000000284949451'},
      {'Customer': u'2000000000084114531'},
      {'Customer': u'1000000000213446086'},
      {'Customer': u'2000000000084114531'},
      {'Customer': u'2000000000127655859'},
      {'Customer': u'1000000000296804864'},
      {'Customer': u'2000000000084114531'},
      {'Customer': u'2000000000127655859'},
      {'Customer': u'2000000000127655859'},
      {'Customer': u'2000000000084114531'},
       {'Customer': u'1000000000213446086'}]
    
      from flask import json
    
    
      In [17]: x = json.dumps(returnObject)
    
      In [18]: x
       Out[18]: '[{"Customer": "2000000000084114531"}, {"Customer":   "1000000000284949451"}, {"Customer": "2000000000084114531"}, {"Customer": "1000000000213446086"}, {"Customer": "2000000000084114531"}, {"Customer": "2000000000127655859"}, {"Customer": "1000000000296804864"}, {"Customer": "2000000000084114531"}, {"Customer": "2000000000127655859"}, {"Customer": "2000000000127655859"}, {"Customer": "2000000000084114531"}, {"Customer": "1000000000213446086"}]'
    

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      相关资源
      最近更新 更多