javascript - 解析 neo4j JSON 响应

标签 javascript json parsing neo4j

我有 neo4j 响应对象(我向您提供了整个响应,所以我会做对):

result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

我只需要从中获取单个“_fields”,如下所示:

{"Perspective", "3D", "2D"}

我该怎么做?

我最终使用这段代码成功地从任何确切的记录中获取了值:

a = Object.values(result.records)
b = Object.values(a[0]._fields)
console.log(b);

但我不明白我怎样才能找到数组中的每个“a”元素并提取“_fields”。

最佳答案

您可以使用 for ... of 循环遍历 records 中的所有对象!

const result = {
    "records": [
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "Perspective"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "3D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        },
        {
            "keys": [
                "criteria.name"
            ],
            "length": 1,
            "_fields": [
                "2D"
            ],
            "_fieldLookup": {
                "criteria.name": 0
            }
        }
    ],
    "summary": {
        "query": {
            "text": "MATCH (criteria:TEST_01)\nRETURN criteria.name\nLIMIT 3",
            "parameters": {}
        },
        "queryType": "r",
        "counters": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "updateStatistics": {
            "_stats": {
                "nodesCreated": 0,
                "nodesDeleted": 0,
                "relationshipsCreated": 0,
                "relationshipsDeleted": 0,
                "propertiesSet": 0,
                "labelsAdded": 0,
                "labelsRemoved": 0,
                "indexesAdded": 0,
                "indexesRemoved": 0,
                "constraintsAdded": 0,
                "constraintsRemoved": 0
            },
            "_systemUpdates": 0
        },
        "plan": false,
        "profile": false,
        "notifications": [],
        "server": {
            "address": "localhost:7687",
            "version": "Neo4j/4.1.0",
            "protocolVersion": 4.1
        },
        "resultConsumedAfter": {
            "low": 2,
            "high": 0
        },
        "resultAvailableAfter": {
            "low": 80,
            "high": 0
        },
        "database": {
            "name": "neo4j"
        }
    }
}

let option1 = [];
let option2 = [];

// For each object in result.records,
for (let val of result.records) {
  // Put everything in val._fields into our result array.
  // ... spreads the array, so all elements are inserted
  // individually in case, in the future,
  // there are multiple items in _fields.
  option1.push(...val._fields);
  
  // For the object you provided, you could just do
  // val._fields[0]. However, option1 is more generalizable
  // in case there's ever more than one thing in _fields.
  option2.push(val._fields[0]);
}

console.log(option1);
console.log(option2);

关于javascript - 解析 neo4j JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67359519/

相关文章:

javascript - Ajax 请求不返回所有 JSON - 无法加载资源 : the server responded with a status of 409 (Conflict)

javascript - Polyline Google Maps V3 中的标签

json - 无法使用Flutter在Dart中写入JSON文件

ruby - 在 Nokogiri 中,如何找到文档中某个节点之前的所有节点?

parsing - 如何从字符串中手动解析 float

c# - 在.NET中读取佳能CR2原始图像文件

javascript - 有没有更好的方法来选择一个不是交互元素的兄弟元素的元素?

javascript - 检测 iframe 何时跨域,然后清除它

javascript - 以更简单的方式获取树数据结构的深度

ios - 尝试在 iOS 中解析动态 JSON