JSON对象解析:REST API neo4j cypher响应中包含多个节点的多行

时间:2015-02-02 11:32:17

标签: json neo4j

当通过REST API玩JSON Neo4j响应时,我试图理解如何访问返回的值, 当我的结果有一列和两行时。每行依次包含多个节点。

查询(给我一个旅行计划):

MATCH (a:LandMark {name:'Acharya College' }),(d:AirPort {name:'Frankfurt International Airport'})
MATCH p1 = allShortestPaths((a)-[:STOPS_AT*]-(d))
WITH p1, FILTER(j IN NODES(p1) WHERE(j in nodes(p1)) ) AS buses
RETURN buses

我理解的响应是嵌套数组,但我无法弄清楚哪些是访问值的索引。

警报(JSON.stringify(data,null,4));给出:

 {    "columns": [        "buses"    ],    "data": [        [            [                {                    "labels": "http://localhost:7474/db/data/node/23/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/23/relationships/out",                    "data": {                        "name": "Acharya College"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/23/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/23",                    "property": "http://localhost:7474/db/data/node/23/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/23/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/23/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/23/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/23/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/23/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 23,                        "labels": [                            "LandMark"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/25/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/25/relationships/out",                    "data": {                        "name": "Auto Rickshaw"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/25/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/25",                    "property": "http://localhost:7474/db/data/node/25/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/25/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/25/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/25/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/25/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/25/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 25,                        "labels": [                            "AutoRickShaw"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/0/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/0/relationships/out",                    "data": {                        "name": "CST International Airport:Mumbai"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/0/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/0",                    "property": "http://localhost:7474/db/data/node/0/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/0/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/0/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/0/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/0/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 0,                        "labels": [                            "AirPort"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/3/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/3/relationships/out",                    "data": {                        "FlightNo": "LF1131"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/3/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/3",                    "property": "http://localhost:7474/db/data/node/3/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/3/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/3/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/3/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/3/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/3/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 3,                        "labels": [                            "AirPlane"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/2/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/2/relationships/out",                    "data": {                        "name": "Frankfurt International Airport"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/2/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/2",                    "property": "http://localhost:7474/db/data/node/2/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/2/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/2/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/2/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/2/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 2,                        "labels": [                            "AirPort"                        ]                    }                }            ]        ],        [            [                {                    "labels": "http://localhost:7474/db/data/node/23/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/23/relationships/out",                    "data": {                        "name": "Acharya College"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/23/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/23",                    "property": "http://localhost:7474/db/data/node/23/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/23/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/23/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/23/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/23/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/23/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/23/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 23,                        "labels": [                            "LandMark"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/25/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/25/relationships/out",                    "data": {                        "name": "Auto Rickshaw"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/25/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/25",                    "property": "http://localhost:7474/db/data/node/25/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/25/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/25/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/25/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/25/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/25/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/25/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 25,                        "labels": [                            "AutoRickShaw"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/0/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/0/relationships/out",                    "data": {                        "name": "CST International Airport:Mumbai"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/0/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/0",                    "property": "http://localhost:7474/db/data/node/0/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/0/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/0/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/0/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/0/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/0/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 0,                        "labels": [                            "AirPort"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/3/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/3/relationships/out",                    "data": {                        "FlightNo": "LF1131"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/3/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/3",                    "property": "http://localhost:7474/db/data/node/3/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/3/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/3/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/3/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/3/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/3/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/3/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 3,                        "labels": [                            "AirPlane"                        ]                    }                },                {                    "labels": "http://localhost:7474/db/data/node/2/labels",                    "outgoing_relationships": "http://localhost:7474/db/data/node/2/relationships/out",                    "data": {                        "name": "Frankfurt International Airport"                    },                    "all_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}",                    "traverse": "http://localhost:7474/db/data/node/2/traverse/{returnType}",                    "self": "http://localhost:7474/db/data/node/2",                    "property": "http://localhost:7474/db/data/node/2/properties/{key}",                    "outgoing_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}",                    "properties": "http://localhost:7474/db/data/node/2/properties",                    "incoming_relationships": "http://localhost:7474/db/data/node/2/relationships/in",                    "extensions": {},                    "create_relationship": "http://localhost:7474/db/data/node/2/relationships",                    "paged_traverse": "http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}",                    "all_relationships": "http://localhost:7474/db/data/node/2/relationships/all",                    "incoming_typed_relationships": "http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}",                    "metadata": {                        "id": 2,                        "labels": [                            "AirPort"                        ]                    }                }            ]        ]    ]}

2 个答案:

答案 0 :(得分:0)

你如何访问它?

http://neo4j.com/developer/javascript/#_using_the_http_endpoint_directly

上查看此javascript代码段
var r=require("request");
var txUrl = "http://localhost:7474/db/data/transaction/commit";
function cypher(query,params,cb) {
  r.post({uri:txUrl,
          json:{statements:[{statement:query,parameters:params}]}},
         function(err,res) { cb(err,res.body)})
}

// Running the Function

var query="MATCH (n:User) RETURN n, labels(n) as l LIMIT {limit}"
var params={limit: 10}
var cb=function(err,data) { console.log(JSON.stringify(data)) }

cypher(query,params,cb)

{"results":[
  {"columns":["n","l"],
   "data":[
     {"row":[{"name":"Aran"},["User"]]}
    ]
  }],
 "errors":[]}

答案 1 :(得分:0)

我知道你正在使用php而你的问题与这个问题有关:Parsing a neo4j JSON object in javascript

为什么不利用现有的驱动程序来消除响应解析的负担。

以下是使用neoclient的示例:

composer require neoxygen/neoclient

-

$client = ClientBuilder::create()
    ->addDefaultLocalConnection()
    ->build();

// Sending your query :
$query = "MATCH (a:LandMark {name:'Acharya College' }),(d:AirPort {name:'Frankfurt International Airport'})
MATCH p1 = allShortestPaths((a)-[:STOPS_AT*]-(d))
WITH p1, FILTER(j IN NODES(p1) WHERE(j in nodes(p1)) ) AS buses
RETURN buses";

$result = $client->sendCypherQuery($query)->getResult();

// Getting the buses

$buses = $result->get('buses'); // Just specify the return alias desired
// Returns you a collection of buses

// Passing what you want to your frontend :

$response = [];
foreach ($buses as $bus) {
    $response[] = [
        'bus_name' => $bus->getProperty('name'),
        'bus_id' => $bus->getProperty('id')
        ];

return json_encode($response);

在你的javascript中你只需要迭代响应; - )