JSON对象解析:REST API neo4j密码响应中具有多个节点的多行 [英] JSON object parsing : multiple rows with multiple nodes in a REST API neo4j cypher response

查看:130
本文介绍了JSON对象解析:REST API neo4j密码响应中具有多个节点的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过REST API处理JSON Neo4j响应时,我想尽力了解如何访问返回的值, 当我的结果是一列两行时.每行依次包含多个节点.

When playing around with JSON Neo4j response over REST API, i am trying to break my head to understand how to should i access a value returned, when my result is having one column and 2 rows . Each row in turn contains multiple nodes.

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

the query (gives me a travel plan) :

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

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

the response i understand is a nested array one, but i am unable to figure out which are the indexes to access the values.

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

the alert(JSON.stringify(data, null, 4)); gives:

 {    "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"                        ]                    }                }            ]        ]    ]}

推荐答案

我知道您正在使用php,并且您的问题与此有关:

I know you're using php and that your question is related to this one : Parsing a neo4j JSON object in javascript

为什么不利用现有的驱动程序来减轻响应解析的负担.

Why don't you take advantage of the existing drivers that will remove you the burden of the response parsing.

以下是使用 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中,您只需要遍历响应即可;-)

And in your javascript you only need to iterate over the response ;-)

这篇关于JSON对象解析:REST API neo4j密码响应中具有多个节点的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆