Is there JSON encode/decode base64 encode/decode function in JavaScript?
-
1possible duplicate of How to base64 encode inside of javascript. Searching is hard, apparently. stackoverflow.com/search?q=javascript+base64Matt Ball– Matt Ball2011-01-12 03:28:03 +00:00Commented Jan 12, 2011 at 3:28
-
take a look at: json.org/js.htmlAlexar– Alexar2011-09-27 03:19:33 +00:00Commented Sep 27, 2011 at 3:19
5 Answers
Yes, btoa() and atob() work in some browsers:
var enc = btoa("this is some text");
alert(enc);
alert(atob(enc));
5 Comments
"Some browsers" === "Gecko and WebKit"JSON and base64 are completely independent.
Comments
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();
};
Comments
For non-Mozilla browsers, use: http://www.webtoolkit.info/javascript-base64.html
For Mozilla browsers, use btoa() and atob().
Comments
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