从Node.js中的JSON数据中提取JSON字段
Extract JSON field from JSON data in Node.js
Hi我有一个名为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] } } ]
我想从这个文件中提取每个text
字段,并将其记录到控制台
现在我正在这样做:
var jsonContent = JSON.parse(body);
jsonContent = JSON.parse(JSON.stringify(jsonContent));
console.log(jsonContent);
但它不起作用。
有人能帮我吗?我正在使用node js,并且一直被困在这个节点上。
如果您从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);
});
- 在不使用lodash-get的情况下从json中提取深层属性
- 如何从两个不同的来源提取JSON数据
- 使用JavaScript在Json中提取时,将数组的元素转换为String
- 提取API对Javascript中JSON对象响应的特定部分
- 在Jquery中从JSON中提取数组值
- 正在从JSON.stringify中提取数据
- 如何将json文件中的数据提取到对象数组中,并在两个控制器之间共享
- 单击按钮即可显示带有提取的json数据的表
- 使用 gulp 将打字稿导出提取到 json 文件
- 如何从生成的JSON数组中提取JSON对象
- 从Node.js中的JSON数据中提取JSON字段
- 使用ng repeat从两个不同的JSON文件中提取数据
- 使用Angularjs提取2个不同的JSON文件
- 使用javascript从JSON中提取数据
- 如何使用循环提取数组中的json数据
- AngularJS/Restangular索引JSON数据提取
- 如何从安卓发送的 json 对象中提取数据
- Json 提取和使用数据(node.js)
- 如何通过json提取Chrome开发工具的网络选项卡内容
- jQuery触发器-只返回一个对象,而不是JSON提取的所有对象