【发布时间】:2017-12-15 00:12:53
【问题描述】:
我有一个通过 node.js 客户端访问的 elasticsearch 数据库。我可以从 elasticsearch 解析 JSON 以打印出 hitsArray,如下所示:
[ { _index: 'parties',
_type: 'suppliers',
_id: 'AV0uELknL82XeGsCOZ-i',
_score: 1,
_source: { name: 'Jabil', address: [Object], rating: 4.2 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_yC3L82XeGsCOZ-f',
_score: 1,
_source: { name: 'Apple', address: [Object], rating: 4.9 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_glkL82XeGsCOZ-d',
_score: 1,
_source: { name: 'Flextronics', address: [Object], rating: 4.5 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_ox7L82XeGsCOZ-e',
_score: 1,
_source: { name: 'FlashMob', address: [Object], rating: 3.5 } } ]
现在,我想解析_source 字段并打印出name 字段
client.search(searchParams).then(function (resp) {
return Promise.all(resp.hits.hits)
}).then(function(hitsArray){
hitsArray.forEach(function(value){
JSONObject first = hitsArray.getJSONObject(value);
JSONObject source = first.getJSONObject("_source");
String name = source.getString("name");
console.log(name);
});
});
但我遇到了错误
JSONObject first = hitsArray.getJSONObject(value);
^^^^^
SyntaxError: Unexpected identifier
我错过了什么?有什么建议吗?
【问题讨论】:
-
你可以使用 underscore.js 来达到这个目的。它已经在那里实施。可能会有所帮助
标签: json node.js elasticsearch