Php 将查询结果转换为Json
我试图找到一种方法,如何将chyper查询的结果转换为Json字符串或对象,不管怎样 我试过几种方法Php 将查询结果转换为Json,php,neo4j,graphaware,Php,Neo4j,Graphaware,我试图找到一种方法,如何将chyper查询的结果转换为Json字符串或对象,不管怎样 我试过几种方法 $client = ClientBuilder::create() ->addConnection('default', 'http://XXX:XXX@XX.XXX.XX.XX:7474') ->build(); $query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100
$client = ClientBuilder::create()
->addConnection('default', 'http://XXX:XXX@XX.XXX.XX.XX:7474')
->build();
$query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";
$result = $client->run($query);
$result->records();
foreach ($result->records() as $record) {
print_r($record);
echo json_encode($record);
}
此时print_r($record)显示数据。
但是,json_encode只返回空对象“{}”。
(1) 我读到关于变量可能受到保护的问题,我发现了这个问题,但解决方案对我不起作用。
(2) 我试图转换变量:
foreach ($result->records() as $record) {
$array = (array) $record;
echo json_encode($array);
}
但效果并不好。与First选项相比,结果稍微好一些:
{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}
但仍然不是很多,也不是我所期望的
(3) 我在neo4j php客户端的源代码中找到了一个名为getJsonResponse()的函数。但我不能称之为程序:
Call to undefined method GraphAware\Neo4j\Client\Formatter\Result::getJsonResponse() in /var/www/html/bpPhpHelper_4.php:15
甚至不知道这是否是一个有用的方法
有客人吗?怎么办?谢谢
这里是打印($record)结构的一小部分:
{"\u0000*\u0000keys":["p"],"\u0000*\u0000values":[{}],"\u0000GraphAware\\Neo4j\\Client\\Formatter\\RecordView\u0000keyToIndexMap":{"p":0}}GraphAware\Neo4j\Client\Formatter\RecordView Object
(
[keys:protected] => Array
(
[0] => p
)
[values:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
(
[nodes:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 190256
[labels:protected] => Array
(
[0] => tibikatPerson
)
[properties:protected] => Array
(
[idTibikatPerson] => 13767
[sonstigeN] => 0
[person] => XXX, XXX
[verfasserN] => 0
[herausgeberN] => 0
[publicationN] => 1
[mitwirkenderN] => 0
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 101334
[labels:protected] => Array
(
[0] => fkatEinzelprojekt
)
[properties:protected] => Array
(
[zeOrt] => Dortmund
[zeBland] => Nordrhein-Westfalen
[ende] => 31.07.2015
[stName] => Gesellschaft für Bildung und Beruf e.V.
[stStadt] => Dortmund
[fSum] => 272104
[lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
[lpsys] => YB1000
[tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A869412949
[fkz] => 01BEX01D12
[zeName] => Gesellschaft für Bildung und Beruf e.V.
[thema] => Verbundprojekt: Aus- und Weiterbildung für die Fachrichtung Tierwirt/in - Schweinehaltung, Kennwort: Tierwirt/in (China), Teilvorhaben: 'Interkulturelle Projektsteuerung und Öffentlichkeitsarbeit'
[stOrt] => Dortmund
[zeStadt] => Dortmund
[beginn] => 01.08.2012
[stBland] => Nordrhein-Westfalen
)
)
[2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 174977
[labels:protected] => Array
(
[0] => fkatVerbundprojekt
)
[properties:protected] => Array
(
[einzelprojekteN] => 5
[ressort] => BMBF
[verbundprojektText] => Tierwirt China
[pt] => PT-DLR
[vkz] => 8732
[fSumTotal] => 792895
[ptEinheit] => BB
)
)
)
[relationships:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 115390
[type:protected] => BETEILIGTER
[startNodeIdentity:protected] => 190256
[endNodeIdentity:protected] => 101334
[properties:protected] => Array
(
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 76338
[type:protected] => UNTERPROJEKT
[startNodeIdentity:protected] => 101334
[endNodeIdentity:protected] => 174977
[properties:protected] => Array
(
)
)
)
)
)
[keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
(
[p] => 0
)
)
对老学徒建议的回答:
代码是:
<?php
require_once 'vendor/autoload.php';
use GraphAware\Neo4j\Client\ClientBuilder;
$client = ClientBuilder::create()
->addConnection('default', 'http://XXX:XXX@XX.XX.XX.XX:7474')
->build();
$query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";
$result = $client->run($query);
$list = array();
$element = array();
foreach ($result->records() as $record) {
// print_r($record);
$element = "$record";
array_push($list,$element);
// $array = (array) $record;
// echo json_encode($array);
}
//echo json_encode($list);
?>
最后一段“{}”是“echo json_encode($record);”的结果,而其余部分是“print_r($record);”的结果,没有报告错误。请检查这个问题的答案,看看它是否有助于正确设置json:declare array/loop/push我想知道我是否正确解释了你的建议。我添加了两个数组。主列表数组($list=array();)和伪数组($element=array();)。对于每个交互,我都尝试将$record转换为字符串($element=“$record”)。我试图将转换后的结果集推入$list数组(array_push($list,$element);)。最后,我尝试通过(json_encode($list))对数组进行JSONE编码。但是,我在记录和字符串之间的转换中遇到了一个错误,即:->GraphAware\Neo4j\Client\Formatter\RecordView类的对象无法转换为字符串。但是谢谢!你能更新代码吗?我更新了。谢谢你的努力我的错误当//回声?当您只运行查询来构造数组时?我的意思是->connect/params/execute/loop(没有其他代码)任何对象名和变量名在某个地方是相同的?请检查这个问题的答案,看看它是否有助于正确设置json:declare array/loop/push我想知道我是否正确解释了你的建议。我添加了两个数组。主列表数组($list=array();)和伪数组($element=array();)。对于每个交互,我都尝试将$record转换为字符串($element=“$record”)。我试图将转换后的结果集推入$list数组(array_push($list,$element);)。最后,我尝试通过(json_encode($list))对数组进行JSONE编码。但是,我在记录和字符串之间的转换中遇到了一个错误,即:->GraphAware\Neo4j\Client\Formatter\RecordView类的对象无法转换为字符串。但是谢谢!你能更新代码吗?我更新了。谢谢你的努力我的错误当//回声?当您只运行查询来构造数组时?我的意思是->connect/params/execute/loop(没有其他代码)任何对象名和变量名在某个地方是相同的吗?
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/* whatever params that are needed to be added here */
$client = ClientBuilder::create()
->addConnection('default', 'http://XXX:XXX@XX.XXX.XX.XX:7474')
->build();
$query = "MATCH p=()-[:BETEILIGTER]->()-[:UNTERPROJEKT]->() RETURN p LIMIT 100";
$result = $client->run($query);
$result->records();
foreach ($result->records() as $record) {
print_r($record);
}
echo json_encode($record);
?>
GraphAware\Neo4j\Client\Formatter\RecordView Object
(
[keys:protected] => Array
(
[0] => p
)
[values:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
(
[nodes:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 196717
[labels:protected] => Array
(
[0] => tibikatPerson
)
[properties:protected] => Array
(
[idTibikatPerson] => 20228
[sonstigeN] => 0
[person] => Wallussek, Ulrich
[verfasserN] => 0
[herausgeberN] => 0
[publicationN] => 2
[mitwirkenderN] => 0
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 100424
[labels:protected] => Array
(
[0] => fkatEinzelprojekt
)
[properties:protected] => Array
(
[zeOrt] => Halblech
[zeBland] => Bayern
[ende] => 30.11.2012
[stName] => ErduMo-S. GmbH & Co. KG - Nebenstelle
[stStadt] => Möhnesee
[fSum] => 147904
[lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
[lpsys] => YB1000
[tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A799010480
[fkz] => BEX017010
[zeName] => ErduMo-S. GmbH & Co. KG
[thema] => Verbundprojekt: Grubensicherheit in chinesischen Steinkohlebergbau, Teilvorhaben: Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich des Arbeits- und Gesundheitsschutzes
[stOrt] => Möhnesee
[zeStadt] => Halblech
[beginn] => 01.10.2010
[stBland] => Nordrhein-Westfalen
)
)
[2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 170018
[labels:protected] => Array
(
[0] => fkatVerbundprojekt
)
[properties:protected] => Array
(
[einzelprojekteN] => 3
[ressort] => BMBF
[vkz] => 3773
[pt] => PT-DLR
[verbundprojektText] => Grubensicherheit im chinesischen Steinkohlebergbau - Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich der Arbeits- und Grubensicherheit
[fSumTotal] => 200623
[ptEinheit] => BB
)
)
)
[relationships:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 152288
[type:protected] => BETEILIGTER
[startNodeIdentity:protected] => 196717
[endNodeIdentity:protected] => 100424
[properties:protected] => Array
(
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 76187
[type:protected] => UNTERPROJEKT
[startNodeIdentity:protected] => 100424
[endNodeIdentity:protected] => 170018
[properties:protected] => Array
(
)
)
)
)
)
[keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
(
[p] => 0
)
)
GraphAware\Neo4j\Client\Formatter\RecordView Object
(
[keys:protected] => Array
(
[0] => p
)
[values:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Path Object
(
[nodes:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 178170
[labels:protected] => Array
(
[0] => tibikatPerson
)
[properties:protected] => Array
(
[idTibikatPerson] => 1681
[sonstigeN] => 0
[person] => Blümke, D.
[verfasserN] => 0
[herausgeberN] => 0
[publicationN] => 2
[mitwirkenderN] => 0
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 100424
[labels:protected] => Array
(
[0] => fkatEinzelprojekt
)
[properties:protected] => Array
(
[zeOrt] => Halblech
[zeBland] => Bayern
[ende] => 30.11.2012
[stName] => ErduMo-S. GmbH & Co. KG - Nebenstelle
[stStadt] => Möhnesee
[fSum] => 147904
[lpysyText] => Austausch mit anderen Staaten im Bereich der beruflichen Bildung sowie Stipendien
[lpsys] => YB1000
[tibikatUrl] => https://www.tib.eu/de/suchen/id/TIBKAT%3A799010480
[fkz] => BEX017010
[zeName] => ErduMo-S. GmbH & Co. KG
[thema] => Verbundprojekt: Grubensicherheit in chinesischen Steinkohlebergbau, Teilvorhaben: Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich des Arbeits- und Gesundheitsschutzes
[stOrt] => Möhnesee
[zeStadt] => Halblech
[beginn] => 01.10.2010
[stBland] => Nordrhein-Westfalen
)
)
[2] => GraphAware\Neo4j\Client\Formatter\Type\Node Object
(
[id:protected] => 170018
[labels:protected] => Array
(
[0] => fkatVerbundprojekt
)
[properties:protected] => Array
(
[einzelprojekteN] => 3
[ressort] => BMBF
[vkz] => 3773
[pt] => PT-DLR
[verbundprojektText] => Grubensicherheit im chinesischen Steinkohlebergbau - Entwicklung und Vermarktung von Ausbildungs- und Weiterbildungsmodulen im Bereich der Arbeits- und Grubensicherheit
[fSumTotal] => 200623
[ptEinheit] => BB
)
)
)
[relationships:protected] => Array
(
[0] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 152287
[type:protected] => BETEILIGTER
[startNodeIdentity:protected] => 178170
[endNodeIdentity:protected] => 100424
[properties:protected] => Array
(
)
)
[1] => GraphAware\Neo4j\Client\Formatter\Type\Relationship Object
(
[id:protected] => 76187
[type:protected] => UNTERPROJEKT
[startNodeIdentity:protected] => 100424
[endNodeIdentity:protected] => 170018
[properties:protected] => Array
(
)
)
)
)
)
[keyToIndexMap:GraphAware\Neo4j\Client\Formatter\RecordView:private] => Array
(
[p] => 0
)
)
{}