嗨,我有一个名为body
的JSON字符串,包含以下内容:[ { _index: 'domain', _type: 'tweets', _id: 'AVQDcXJkNcXkyWvNoI-S', _score: 1.3844389, _source: { username: '[email protected]', text: '@amrightnow Go Trump Go Trump Go Trump', location: [Object] } }, { _index: 'domain', _type: 'tweets', _id: 'AVQDe5CONcXkyWvNoI_G', _score: 0.98909086, _source: { username: 'Trump Hotels Jobs', text: '#Vancouver, BC #CustomerService #Job: Reservations Agent at Trump ', location: [Object] } }, { _index: 'domain', _type: 'tweets', _id: 'AVQDfDpfNcXkyWvNoI_L', _score: 0.5487978, _source: { username: '☩Chaunce☩', text: 'While figuring out what 2 do next, #Trump complains 2 his rep about not winning a "popcorn" it seems he\'ll go after the #MTVMovieAwards too', location: [Object] } } ]
我想从该文件中提取每个text
字段,并将其记录到控制台。
现在我正在这样做:var jsonContent = JSON.parse(body); jsonContent = JSON.parse(JSON.stringify(jsonContent)); console.log(jsonContent);
但这不起作用。
有人可以帮我吗?我正在使用节点js,并被困在这一方面。
最佳答案
如果您以json字符串格式的对象开头,那么这里是一个完整的解决方案
var jsonString = "[{\"_index\":\"domain\",\"_type\":\"tweets\",\"_id\":\"AVQDcXJkNcXkyWvNoI-S\",\"_score\":1.3844389,\"_source\":{\"username\":\"[email protected]\",\"text\":\"@amrightnow Go Trump Go Trump Go Trump\",\"location\":[null]}},{\"_index\":\"domain\",\"_type\":\"tweets\",\"_id\":\"AVQDe5CONcXkyWvNoI_G\",\"_score\":0.98909086,\"_source\":{\"username\":\"Trump Hotels Jobs\",\"text\":\"#Vancouver, BC #CustomerService #Job: Reservations Agent at Trump \",\"location\":[null]}},{\"_index\":\"domain\",\"_type\":\"tweets\",\"_id\":\"AVQDfDpfNcXkyWvNoI_L\",\"_score\":0.5487978,\"_source\":{\"username\":\"☩Chaunce☩\",\"text\":\"While figuring out what 2 do next, #Trump complains 2 his rep about not winning a \\\"popcorn\\\" it seems he'll go after the #MTVMovieAwards too\",\"location\":[null]}}]";
var jsonObj = JSON.parse(jsonString);
jsonObj.map((elt) => {
console.log(elt["_source"].text);
});
关于javascript - 从Node.js中的JSON数据中提取JSON字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36540193/