首先,提前感谢您阅读本文。 其次,对于这篇长文章提前致歉 - 但我希望像一个写得很好的函数一样,好东西在顶部 - 尽管请花时间阅读所有内容。 第三,我忘记了我查看了多少堆栈问题 - 如果这仍然是一个菜鸟问题,我深表歉意。
我正在使用 Node.js 为我的 AngularJS 客户端提供 API。我所有的数据聚合和转换都将在 Node 中完成,并向客户端呈现一个平面 JSON 数据结构。
我有一个 Neo4j 模型,它将我与我负责的应用程序相关联,并将技术风险与所有应用程序相关联。我有一个很好的 Cypher 查询,它只显示我负责的应用程序的技术风险,并排除我所有其他没有风险的应用程序。这是我的查询结果的图片: Neo4j Graph result .
这是我的 node.js 代码(主要 api.js 文件调用的文件 routes.js):
var router = require('express').Router();
var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://user:password@localhost:7474');
router.get('/techrisks', getTechRisks);
module.exports = router;
function getTechRisks(req, res) {
db.cypher({
query: 'MATCH p=(a)-[e:ARCHITECT_FOR]->(b)-[d:HAS_RISK]->(c) WHERE a.shortname="macdonb" RETURN nodes(p) AS n,relationships(p) AS m',
params: {
}
}, function (err, results) {
if (err) { throw err; }
var result = results[0];
if (!result) {
console.log('No TechRisk found.');
} else {
console.log(results);
console.log(results[0]);
console.log(results[1]);
}
});
}
上面的代码生成以下 JSON(为了便于阅读,在 block 中添加了分隔符) 产生这些结果:
- - - - console.log(results); - - - -
[ { n: [ [Object], [Object], [Object] ],
m: [ [Object], [Object] ] },
{ n: [ [Object], [Object], [Object] ],
m: [ [Object], [Object] ] },
{ n: [ [Object], [Object], [Object] ],
m: [ [Object], [Object] ] },
{ n: [ [Object], [Object], [Object] ],
m: [ [Object], [Object] ] },
{ n: [ [Object], [Object], [Object] ],
m: [ [Object], [Object] ] } ]
- - - - console.log(results[0]); - - - -
{ n:
[ Node { _id: 585, labels: [Object], properties: [Object] },
Node { _id: 675, labels: [Object], properties: [Object] },
Node { _id: 695, labels: [Object], properties: [Object] } ],
m:
[ Relationship {
_id: 845,
type: 'ARCHITECT_FOR',
properties: [Object],
_fromId: 585,
_toId: 675 },
Relationship {
_id: 813,
type: 'HAS_RISK',
properties: [Object],
_fromId: 675,
_toId: 695 } ] }
- - - - console.log(results[1]); - - - -
{ n:
[ Node { _id: 585, labels: [Object], properties: [Object] },
Node { _id: 674, labels: [Object], properties: [Object] },
Node { _id: 689, labels: [Object], properties: [Object] } ],
m:
[ Relationship {
_id: 844,
type: 'ARCHITECT_FOR',
properties: [Object],
_fromId: 585,
_toId: 674 },
Relationship {
_id: 810,
type: 'HAS_RISK',
properties: [Object],
_fromId: 674,
_toId: 689 } ] }
n: 数组让我感到困惑。我开始手动将它拆开(尝试分别使用字符串和对象进行拆分和拼接),但我相信它必须可以使用 json 解析器访问。当我使用以下内容作为测试时:
var tmpResult1 = JSON.stringify(result.n);
我得到了 n 的一个很好的字符串表示:
[{"_id":585,"labels":["Person"],"properties":{"hrpno":"00061627","lastName":"MacDonald","title":"Consultant, IT Architecture","hrdno":"104134","shortname":"macdonb","role":"Systems Architect","displayName":"Bruce MacDonald","firstName":"Bruce"}},{"_id":650,"labels":["Application"],"properties":{"dateverified":"2016-01-19","name":"Portal - Loss Runs","aprmid":"aprm1249"}},{"_id":683,"labels":["TechRisk"],"properties":{"status":"Documented","riskid":"ABC-2012-082","dateEntered":"2012-06-29"}}]
这个字符串很好,但是当我尝试用点或括号引用数组时,我得到了很多“未定义”的错误。
我有点迷茫,准备再休息一两天。我在 YouTube 上查找教程 - “大约 49,300 个结果”(!!!),在我看过的 30 多个结果中,它们都处理简单的结构并以“电影”为例。
再次感谢您阅读到这里!感谢任何帮助或线索。
最佳答案
我专门为此编写了一个库:
https://www.npmjs.com/package/parse-neo4j
它解析 Neo4j 的输出并仅提取查询中返回的内容,看看它是否有帮助!
Neo4j's http endpoint produces result that contains complete query information.
parse-neo4j helps those who only want what they've returned in the query as normal JSON.
关于javascript - 从 Neo4j 解析复杂的 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230458/