Encode JSON object to base 64

时间:2017-12-18 07:34:26

标签: javascript json base64

I have a following JSON object :

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="master.css">
  </head>
  <body>
    <table id="rows">
    </table>
  </body>
 <script type="text/javascript">
 let table = document.getElementById("rows")
                             let row = table.insertRow();
                             let cell_1 = row.insertCell();
                             cell_1.innerHTML = "cell1";

                             let cell_2 = row.insertCell();
                             cell_2.innerHTML = "cell2";

                             var inputElement = document.createElement('input');
                             inputElement.type = 'button';
                             inputElement.addEventListener('click', function() {
                                 alert("It Works here");
                             });

                             let cell_3 = row.insertCell();
                             cell_3.appendChild(inputElement);

 </script>
</html>

Resulting encoded base 64 string :
DQp7DQogICJjYXNlSWQiOiAiNDI2MDgyMzciLA0KICAiY2FzZU5vIjogIjUwMDFnMDAwMDAwZmdocUFBQSIsDQogICJkZXNjcmlwdGl2ZU5hbWUiOiAidGVzdCIsDQogICJhZGROb3RlIjogInRlc3QiDQp9

I want to encode the above object to base 64.
I tried to convert the object to string first and then use btoa function on the string to encode it. Is it the right approach?

0 个答案:

没有答案