JSON encode/decode base64 encode/decode in JavaScript

走远了吗. 提交于 2019-12-31 08:46:26

问题


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


回答1:


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

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



回答2:


JSON and base64 are completely independent.

Here's a JSON stringifier/parser (and direct GitHub link).

Here's a base64 Q&A. Here's another one.




回答3:


This might be helpful for you. Using a combination of this project crypto-js and 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)


    function JSONtoBase64(jsonObj) {
        return Crypto.util.bytesToBase64(Crypto.charenc.UTF8.stringToBytes(Object.toJSON(jsonObj)));
    };

    function base64ToJSON(bytes) {
        var jsonString = Crypto.charenc.UTF8.bytesToString(Crypto.util.base64ToBytes(bytes));
        return jsonString.evalJSON();
    };




回答4:


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

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




回答5:


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

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



来源:https://stackoverflow.com/questions/4665049/json-encode-decode-base64-encode-decode-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!