如何将 json.stringify 响应存储到文件中?
How to store json.stringify response into a file?
目前我正在做的 node.js 项目有问题,但我不知道如何将从下面的代码中得到的 json.stringify 响应存储在一个文件中从我可以访问 jSON 数据的地方,或者有没有更好的方法可以在不将其存储在文件中的情况下执行此操作。
这是我的代码:
var watson = require('watson-developer-cloud');
var personality_insights = watson.personality_insights({
username: '...',
password: '...',
version: 'v2'
});
personality_insights.profile({
text: 'I write this to explain why I’ll be holding back my album, 1989, from the new streaming service, Apple Music. I feel this deserves an explanation because Apple has been and will continue to be one of my best partners in selling music and creating ways for me to connect with my fans. I respect the company and the truly ingenious minds that have created a legacy based on innovation and pushing the right boundaries.I’m sure you are aware that Apple Music will be offering a free 3 month trial to anyone who signs up for the service. I’m not sure you know that Apple Music will not be paying writers, producers, or artists for those three months. I find it to be shocking, disappointing, and completely unlike this historically progressive and generous company.This is not about me. Thankfully I am on my fifth album and can support myself, my band, crew, and entire management team by playing live shows. This is about the new artist or band that has just released their first single and will not be paid for its success.',
language: 'en'
}, function (err, response) {
if (err) {
console.log('error:', err);
} else {
console.log( JSON.stringify(response, null, 2) );
}
});
});
我在我的代码中使用上面的 watson developer cloud。
是这样的吗?
var watson = require('watson-developer-cloud');
var fs = require('fs');
var personality_insights = watson.personality_insights({
username: '...',
password: '...',
version: 'v2'
});
personality_insights.profile({
text: 'blah blah blah',
language: 'en'
}, function (err, response) {
if (err) {
console.log('error:', err);
} else {
fs.writeFile(
'./file.json',
JSON.stringify(response, null, 2),
function(err){
throw err;
}
);
}
});
});
目前我正在做的 node.js 项目有问题,但我不知道如何将从下面的代码中得到的 json.stringify 响应存储在一个文件中从我可以访问 jSON 数据的地方,或者有没有更好的方法可以在不将其存储在文件中的情况下执行此操作。
这是我的代码:
var watson = require('watson-developer-cloud');
var personality_insights = watson.personality_insights({
username: '...',
password: '...',
version: 'v2'
});
personality_insights.profile({
text: 'I write this to explain why I’ll be holding back my album, 1989, from the new streaming service, Apple Music. I feel this deserves an explanation because Apple has been and will continue to be one of my best partners in selling music and creating ways for me to connect with my fans. I respect the company and the truly ingenious minds that have created a legacy based on innovation and pushing the right boundaries.I’m sure you are aware that Apple Music will be offering a free 3 month trial to anyone who signs up for the service. I’m not sure you know that Apple Music will not be paying writers, producers, or artists for those three months. I find it to be shocking, disappointing, and completely unlike this historically progressive and generous company.This is not about me. Thankfully I am on my fifth album and can support myself, my band, crew, and entire management team by playing live shows. This is about the new artist or band that has just released their first single and will not be paid for its success.',
language: 'en'
}, function (err, response) {
if (err) {
console.log('error:', err);
} else {
console.log( JSON.stringify(response, null, 2) );
}
});
});
我在我的代码中使用上面的 watson developer cloud。
是这样的吗?
var watson = require('watson-developer-cloud');
var fs = require('fs');
var personality_insights = watson.personality_insights({
username: '...',
password: '...',
version: 'v2'
});
personality_insights.profile({
text: 'blah blah blah',
language: 'en'
}, function (err, response) {
if (err) {
console.log('error:', err);
} else {
fs.writeFile(
'./file.json',
JSON.stringify(response, null, 2),
function(err){
throw err;
}
);
}
});
});