JavaScript 中的 JSON 编码/解码 base64 编码/解码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665049/
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-08-23 13:32:11  来源:igfitidea点击:

JSON encode/decode base64 encode/decode in JavaScript

javascriptjsonbase64

提问by theHack

Is there JSON encode/decode base64 encode/decode function in JavaScript?

JavaScript 中是否有 JSON 编码/解码 base64 编码/解码功能?

采纳答案by david

Yes, btoa() and atob() work in some browsers:

是的, btoa() 和 atob() 在某些浏览器中工作:

var enc = btoa("this is some text");
alert(enc);
alert(atob(enc));

回答by Matt Ball

回答by Diego Marafetti

This might be helpful for you. Using a combination of this project crypto-jsand Prototype to parse JSON I wrote two function to encode/decode JSON to Base 64. (These functions don't check for not well formatted json)

这可能对你有帮助。使用这个项目的组合crypto-js和 Prototype 来解析 JSON 我写了两个函数来编码/解码 JSON 到 Base 64。(这些函数不检查格式不正确的 json)

    函数 JSONtoBase64(jsonObj) {
        返回 Crypto.util.bytesToBase64(Crypto.charenc.UTF8.stringToBytes(Object.toJSON(jsonObj)));
    };

    函数 base64ToJSON(字节){
        var jsonString = Crypto.charenc.UTF8.bytesToString(Crypto.util.base64ToBytes(bytes));
        返回 jsonString.evalJSON();
    };

回答by David Titarenco

For non-Mozilla browsers, use: http://www.webtoolkit.info/javascript-base64.html

对于非 Mozilla 浏览器,请使用:http: //www.webtoolkit.info/javascript-base64.html

For Mozilla browsers, use btoa()and atob().

对于 Mozilla 浏览器,请使用btoa()atob()

回答by Andy

I don't think there's one built in, but here's the functions for JSON in jquery: (can't post links since I'm new)
jQuery.getJSON
jQuery.parseJSON

我不认为有一个内置的,但这里是 jquery 中的 JSON 函数:(因为我是新手,所以不能发布链接)
jQuery.getJSON
jQuery.parseJSON

and here's a link for base64 encoding in javascript.
http://www.webtoolkit.info/javascript-base64.html

这是javascript中base64编码的链接。
http://www.webtoolkit.info/javascript-base64.html