javascript nodejs中的base64 JSON编码字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22515180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:17:34  来源:igfitidea点击:

base64 JSON encoded strings in nodejs

javascriptjsonnode.jsbase64buffer

提问by ThomasReggi

How do I create a base64 JSON encoded strings in nodejs?

如何在 nodejs 中创建 base64 JSON 编码的字符串?

I tried this and it didn't work.

我试过这个,但没有用。

var buff = new Buffer({"hello":"world"}).toString("base64");

Is this it?

是这个吗?

var buff = new Buffer(JSON.stringify({"hello":"world"})).toString("base64");

回答by ThomasReggi

var buff = new Buffer(JSON.stringify({"hello":"world"})).toString("base64");

回答by cpres

To complete @ladenedge's comment for clarity reasons:

为清楚起见,完成@ladenedge 的评论:

var buff = Buffer.from(JSON.stringify({"hello":"world"})).toString("base64")

var buff = Buffer.from(JSON.stringify({"hello":"world"})).toString("base64")