需要将Mysql数据写入json文件

时间:2017-02-22 20:17:25

标签: mysql json node.js

我正在尝试从mysql数据库中读取数据并将其写入JSON文件。我是node.js的新手,非常感谢任何帮助。

我写的代码:

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
dates =  ['2014-12-15','2015-11-07','2016-01-10']
x = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.stackplot(x,[1,2,3],[7,8,3])
plt.gcf().autofmt_xdate()
plt.show()

现在使用"节点filename.js"运行代码时能够将输出打印到我的控制台。但我希望它写入一个json文件,以便我可以处理数据并应用一些逻辑。

1 个答案:

答案 0 :(得分:0)

如果您只想将JSON中的MySQL数据输出写入文件,请替换...

for (var i in rows) {
 console.log('the rows are', rows[i]);
}

...与:

fs.writeFile("/path/to/file.json", JSON.stringify(rows), function(err) {
    if (err) return console.log(err);    
    console.log("The file was saved!");
});

有关https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

JSON.stringify()的更多信息