从Neo4jparsing复杂的JSON结果

首先,感谢您阅读本文。 其次,对于这篇长文章,提前感到抱歉 – 但是我希望写一篇function良好的文章,好东西是最重要的,尽pipe请花点时间阅读。 第三,我失去了多less堆栈问题的踪迹 – 如果这仍然是一个白费问题,我很抱歉。

我正在使用Node.js为我的AngularJS客户端提供api。 我所有的数据聚合和转换都将在Node中完成,并向客户端呈现一个扁平的JSON数据结构。

我有一个Neo4j模型,将我与我负责的应用程序联系起来,并将技术风险与所有应用程序相关联。 我有一个很好的Cypher查询,只显示我负责的应用程序的技术风险,排除所有其他没有风险的应用程序。 这里是我的查询结果的图片: Neo4j图表结果 。

这里是我的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(为了便于阅读,将分隔符添加到块中)生成以下结果:

 - - - - 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:数组混淆了我。 我开始手动拆分它(分别试图拆分和拼接string和对象),但我相信它必须可以使用JSONparsing器访问。 当我使用以下作为testing:

 var tmpResult1 = JSON.stringify(result.n); 

我得到了一个伟大的string表示的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"}}] 

该string是好的,但是当我尝试用点或括号引用数组时,我得到了很多“未定义”的错误。

我有点失落,准备再花一两天时间rest一下。 我在YouTube上看过教程 – “大约有49,300个结果”(!!!),而我看过的30多个结果都是以简单的结构来处理的,并以“电影”为例。

再一次感谢您阅读这个! 我感谢任何帮助或线索。

我觉得你对console.log()输出感到困惑,它只是一个简短的表示,它并不总是显示整个对象(例如[Object]意味着有一个内部的复杂对象)。 results是一个对象不是JSONstring,做一个console.log(typeof results); 它会输出object

你可以使用console.log(JSON.stringify(results, null, 3)); 要显示整个对象,它将帮助您通过查看所需属性的确切位置(仅用于debugging)来遍历对象。 接下来,由于result是一个有效的JS对象, result不需要parsing或拼接/拆分任何东西,只是使事情变得更加困难。 例如,尝试console.log(results[0].n[0].properties.lastName); ,它应该显示MacDonald

我专门为此写了一个库:

https://www.npmjs.com/package/parse-neo4j

它分析Neo4j的输出,只提取查询中返回的内容,看看是否有帮助!

Neo4j的http端点生成包含完整查询信息的结果。

parse-neo4j可以帮助那些只想在查询中返回正常JSON的用户。

经过一个好的睡眠,我想我会采取另一个裂缝在这个。 我逐渐走过对象数组对象的数组…这里是我最终以 – 这是我的新的条件…

编辑:我忘了提到,我结束了使用lodash图书馆。

 } else { var TechRisk = []; for ( var i = 0, len = results.length; i < len; ++i) { TechRisk.push(_.defaults(_.get((_.get(results[i], 'n')[0]), 'properties'),_.get((_.get(results[i], 'n')[1]), 'properties'),_.get((_.get(results[i], 'n')[2]), 'properties'),_.get((_.get(results[i], 'm')[0]), 'properties'),_.get((_.get(results[i], 'm')[1]), 'properties'))); } res.status(200).send(TechRisk); } 

如果有人有一个更优雅的方式来处理我的嵌套对象组,让我知道。 这是一个蛮力,是非常脆弱的(我的Cypher查询需要使用参数,所以我可以通过不同的用户等)。

对于我在开发周期中的位置,我正在向前迈进。

干杯!