简体   繁体   English

从Node.js中的JSON数据中提取JSON字段

[英]Extract JSON field from JSON data in Node.js

Hi I have a JSON string named body with following contents: 嗨,我有一个名为body的JSON字符串,具有以下内容:

[ { _index: 'domain', _type: 'tweets', _id: 'AVQDcXJkNcXkyWvNoI-S', _score: 1.3844389, _source: { username: 'rosscmary@gmail.com', 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] } } ]

I want to extract every text field from this file and log it to the console. 我想从该文件中提取每个text字段,并将其记录到控制台。

Right now I am doing this : 现在我正在这样做:

var jsonContent = JSON.parse(body); jsonContent = JSON.parse(JSON.stringify(jsonContent)); console.log(jsonContent);

But it's not working. 但这不起作用。

Can any body help me with this ? 有人可以帮我吗? I am using node js and been stuck on this one. 我正在使用节点js,并被困在这一方面。

Here a complete solution if you start with your object in json string format 如果您以json字符串格式的对象开头,那么这里是一个完整的解决方案

 var jsonString = "[{\\"_index\\":\\"domain\\",\\"_type\\":\\"tweets\\",\\"_id\\":\\"AVQDcXJkNcXkyWvNoI-S\\",\\"_score\\":1.3844389,\\"_source\\":{\\"username\\":\\"rosscmary@gmail.com\\",\\"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); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM