json - 使用 Watson API Nodejs 分析 json

标签 json node.js web ibm-watson tone-analyzer

我想分析使用 Watson 音调分析器动态创建的 JSON 文件。我希望它读取文件,然后分析它。

如何让tone_analyzer.tone方法读取文件?谢谢。

app.get('/results', function(req, res) {

    // This is the json file I want to analyze
    fs.readFile('./output.json', null, cb);

    function cb() {
        tone_analyzer.tone({
        // How can I pass the file here?
                text: ''
            },
            function(err, tone) {
                if (err)
                    console.log(err);
                else
                    console.log(JSON.stringify(tone, null, 2));
            });
        console.log('Finished reading file.')
    }
    res.render('results');
})

最佳答案

您的回调缺少几个参数(错误、数据)(有关详细信息,请参阅 the node fs documentation)。数据是文件的内容,将发送到您发送文本的位置。

尝试这样的事情:

app.get('/results', function(req, res) {

    // This is the json file I want to analyze
    fs.readFile('./output.json', 'utf8', cb);

    function cb(error, data) {
        if (error) throw error;
        tone_analyzer.tone({
        // How can I pass the file here?
                text: data
            },
            function(err, tone) {
                if (err)
                    console.log(err);
                else
                    console.log(JSON.stringify(tone, null, 2));
            });
        console.log('Finished reading file.')
    }
    res.render('results');
})

关于json - 使用 Watson API Nodejs 分析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879436/

相关文章:

node.js - 尝试从 'rxjs/operators' 导入 {map} 时,不支持目录导入解析 ES 模块;

node.js - 当路由参数添加到 req.params 时?

java - 除了 "dev"之外,Spring redirectAttributes 无法在其他配置文件中工作

php - 如何在 php 中循环更新查询并显示它,使其像日志历史记录一样

javascript - HTML 部分 100% 视口(viewport)高度

sql - Azure 表单识别对 Microsoft SQL 表的 JSON 响应

json - 使用 Mule 中的 Dataweave 将定界文件转换为 JSON 格式

node.js - Alamofire图像代码=-1016 "Failed to validate response due to unacceptable content type"

java - 在自定义反序列化器中调用默认反序列化器不会影响实例

json - 更新了 Netflix 的电视节目列表