从请求Url中读取json文件

时间:2018-03-06 10:17:45

标签: node.js express

我正在尝试从此网址中提取数据

  

https://twitter.com/i/tfb/v1/tweet_activity/web/poll/968074591301140480

当我在浏览器中点击此网址时,开始下载.json文件。

但我想在node.js中读取这些数据。

我陷入了困境。 请帮帮我。

1 个答案:

答案 0 :(得分:1)

很简单,只需下载文件并执行此操作:

var json = require('./yourfile.json');

Node会自动解析这个json,现在你可以访问它了。

console.log(json);

Here's one example, i did myself.

现在,如果您想动态地执行此操作,我的意思是对于node.js中的用户:

你可以这样做:

首先安装一个http中间件,例如node-fetch

npm install node-fetch

现在你进入节点并运行它:

var fetch = require('node-fetch');



fetch(`https://twitter.com/i/tfb/v1/tweet_activity/web/poll/USER_ID_HERE`)
.then(res => res.json)
.then((json) => {
      // u have your json here now, u can use it here.
 })