JSON encode/decode base64 encode/decode in JavaScript

后端 未结 5 2074
面向向阳花
面向向阳花 2021-01-31 18:44

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

5条回答
  •  北海茫月
    2021-01-31 18:54

    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();
        };
    
    

提交回复
热议问题